- Home /
Deserialize nested Json data
Hi. Please, could somebody help deserialize JSON from a file. I have no chance to do it alone. The result of the script is a empty dictionary.
P. S. I need to do this to make setting up of troops in unity editor faster
[System.Serializable]
public class TroopValues
{
public int dps;
public int health;
public string name;
public int price;
}
[System.Serializable]
public class LevelList
{
public List<TroopValues> list = new List<TroopValues>();
}
[System.Serializable]
public class TroopsList
{
public Dictionary<string, LevelList> dict = new Dictionary<string, LevelList>();
public static TroopsList CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<TroopsList>(jsonString);
}
}
// JSON (it is only a part of a json file)
{
"MHN-1": [
"{\n \"dps\": 10,\n \"health\": 150,\n \"name\": \"MHN-1\",\n \"price\": 10,\n",
"{\n \"dps\": 20,\n \"health\": 200,\n \"name\": \"MHN-1\",\n \"price\": 20,\n",
"{\n \"dps\": 30,\n \"health\": 250,\n \"name\": \"MHN-1\",\n \"price\": 30,\n}"
]
}
Comment
Best Answer
Answer by haruna9x · Sep 13, 2017 at 07:22 AM
JsonUtility uses Unity's serialization system, which does not include a dictionary. Replace it with a list or array. Or use another plugin.