0

i'm trying to change a JSONNode into a Dictionnary. I've been searching for hours for a solution. I know that JsonUtilities doesn't work with Dictionaries. Everything I try gives test4 = null with no errors.

JSONNode test1 = JSON.Parse(customData);

//customData is :"{\"ItemClass\":\"S\",\"Type\":\"Band\",\"Stats\":[{\"HP\":\"100\",\"Atk\":\"50\",\"Def\":\"25\",\"Crit\":\"10\"}]}"

               
JSONArray test2 = test1["Stats"].AsArray;

               
JSONNode  test3 = test2[0];

TEST4 always gives me a null, I tried changing test3 into an Object or string as well. Here are some of the stuff I tried:

var test4 = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(test3);

var test4 = JsonConvert.DeserializeObject<List<Dictionary<string, int>>>(test3);

var test4 = JsonConvert.DeserializeObject<Dictionary<string, string>(test3);

var test4 = JsonConvert.DeserializeObject<Dictionary<string, int>(test3);

I'm trying to reach the Dictionaries that are in the picture below

https://i.stack.imgur.com/2JWLB.png

I'm sure I'm missing something super simple....

Thanks

Xerillio
  • 4,855
  • 1
  • 17
  • 28
Patrick
  • 3
  • 2
  • this answer shows precisely how to move json to Dictionary in Unity https://stackoverflow.com/a/38535392/294884 – Fattie Feb 20 '21 at 18:57
  • Instead of directly serializing a dictionary then deserializing it, I suggest you to create list for both keys and values. When you want to use dictionary from JSON, add lists to an empty dictionary. – SeLeCtRa Feb 20 '21 at 19:09
  • @SeLeCtRa I would suggest the opposite and rather directly use `Dictionary` which is fully supported by `Newtonsoft .Net JSON` – derHugo Feb 20 '21 at 19:21
  • Isn't there a way to get to the dictionary in test3 (like in the picture) ? The examples in the link provided uses classes were as my example, there isn't really classes....maybe I'm not getting something. – Patrick Feb 20 '21 at 19:33

1 Answers1

0

You would rather use the proper data structure representation of your json (e.g. using json2csharp)

[Serializable]
public class Root    
{
    public string ItemClass; 
    public string Type; 
    public List<Dictionary<string,string>>; 
}

And then do

var root = JsonConvert.DeserializeObject<Root>(jsonString); 

foreach(var stat in root.Stats)
{
    foreach(var kvp in stat)
    {
        Debug.Log($"{kvp.Key}:{kvp.Value}");
    } 
}

Or alternatively not even use a Dictionary but directly a class like

[Serializable]
public class Stat
{
    public string HP; 
    public string Atk; 
    public string Def; 
    public string Crit; 
}

[Serializable]
public class Root
{
    public string ItemClass; 
    public string Type; 
    public List<Stat> Stats; 
}

Then you could either use

var root = JsonConvert.DeserializeObject<Root>(jsonString); 

or also built-in

var root = JsonUtility.FromJson<Root>(jsonString);

and then do e.g.

foreach(var stat in root.Stats)
{
    Debug.Log($"HP:{stat.HP}, Ark:{stat.Atk}, Def:{stat.Def}, Crit:{stat.Crit}");
}

Note: typed on smartphone but I hope the idea gets clear

derHugo
  • 83,094
  • 9
  • 75
  • 115