0
\$\begingroup\$

I am creating nested JSON and binding it with the class. But It doesn't bind with the class.

JSON:

 {
  "PuzzleData": {
    "Category1": {
      "Cultus": {
        "Type": "Car",
        "Name": "Cultus",
        "Item1": "gate",
        "Item2": "window",
        "Item3": "tyre",
        "Item4": "tyre"
      },
      "Lion": {
        "Type": "Lion",
        "Name": "Animal",
        "Item1": "legs",
        "Item2": "hands",
        "Item3": "head",
        "Item4": "eyes"
      }
    }
  }
}

Class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;

[System.Serializable]
public class PuzzleGameData{

    public Dictionary<string, Category1> PuzzleData;


    [System.Serializable]
    public class Category1
    {
        public string Type;
        public string Name;
        public string Item1;
        public string Item2;
        public string Item3;
        public string Item4;
    }
}

Any help would be appreciated. Thanks.

\$\endgroup\$
2
  • \$\begingroup\$ What's the error? Which JSON library are you using? \$\endgroup\$
    – Iggy
    May 5, 2017 at 13:44
  • \$\begingroup\$ @Iggy I can't bind JSON with the class. I can bind simple JSON but not nested JSON. \$\endgroup\$
    – Saad Anees
    May 6, 2017 at 8:12

1 Answer 1

1
\$\begingroup\$

Currently Unity 5's JSON utility is not fully capable of doing all the neat tricks you might expect. For instance, it can't do an array of objects that contain other arrays. You can hack the json string with wrapper classes to make it work for that case. For your Dictionary class it may be the same issue. I don't know how to hack that to make it work, but here is an example of using wrapper classes to make arrays within arrays work.

  public string CreateJSONStringFromArray(SaveableObject[] save_obj_array)
    {


        string building_json_string = string.Empty;

        for (int i = 0; i < save_obj_array.Length; i++)
        {

            if (building_json_string != string.Empty) building_json_string = building_json_string + ",";

            string js_converted = JsonUtility.ToJson(save_obj_array[i] );

            building_json_string = building_json_string + js_converted;
        }

        //this is the important part right here
        building_json_string = "{\"Items\":[" + building_json_string + "]}";
        return building_json_string;
    }


    //for reading the values back from json
    public static T[] FromJson<T>(string json)
    {
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
        return wrapper.Items;
    }

    public static string ToJson<T>(T[] array)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return UnityEngine.JsonUtility.ToJson(wrapper);
    }

    //for reading the values back from json
    [Serializable]
    private class Wrapper<T>
    {
        public T[] Items;
    }


}

[System.Serializable]
public class SaveableObject
{
    public string object_name_to_save;
    public string something_else_to_save;
    public string[] items;//with an internal list.

}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the answer. You were right about wrapping. I used my own wrapper class. \$\endgroup\$
    – Saad Anees
    May 6, 2017 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .