0

I was practising JSON Deserialization using JSONUtility in Unity engine.I solved some examples using POCO class when the properties were string with help of stackoverflow(Link).I have solved the following JSON.Link using the exact names in the POCO class.But when the JSON properties contains integers like the following .

{


"1": {
    "Armor": 1, 
    "Strenght": 1
  }, 
  "0": {
    "Armor": 1, 
    "Mana": 2, 
    "Strenght": 1, 
    "Health": 1, 
    "Power": 1
  }
}

Solving in a similar way did not work out.From the feedback I got,I have to use dictionary.

 IEnumerator GetExampleValues()
{
    string URLss = "http://www.json-generator.com/api/json/get/bOQGnptixu?indent=2";
    WWW readjson = new WWW(URLss);
    yield return readjson;

    Debug.Log("ReadJSON"+readjson.text);


}

My POCO class below

using System.Collections.Generic;

[System.Serializable]
public class Exampleget  
{
    public int Power;
    public int Mana;
    public int Strenght;
    public int Armor;
    public int Health;

}

public class GetNumbers
{

    public List<Exampleget> onevalues;
}
zyonneo
  • 1,319
  • 5
  • 25
  • 63

1 Answers1

0

One way to approach this would be using JsonExtensionData Attribute

public class Exampleget  
{
    public int Power;
    public int Mana;
    public int Strenght;
    public int Armor;
    public int Health;

}

public class GetNumbers
{
    [JsonExtensionData]
    public Dictionary<string,dynamic> Values;
}

With the Class in place, now you could,

var str = @"{
        '1': {
            'Armor': 1, 
            'Strenght': 1
          }, 
          '0': {
            'Armor': 1, 
            'Mana': 2, 
            'Strenght': 1, 
            'Health': 1, 
            'Power': 1
          }
        }";

var result = JsonConvert.DeserializeObject<GetNumbers>(str);
foreach(var item in result.Values)
{
    Console.WriteLine($"For Key {item.Key},Power = {item.Value.Power}, Mana = {item.Value.Mana}, Armor = {item.Value.Armor}, Health = {item.Value.Health}");
}

Output

For Key 1,Power = , Mana = , Armor = 1, Health = 
For Key 0,Power = 1, Mana = 2, Armor = 1, Health = 1

Update

In case you want to have the Dictionary as Dictionary<string,Exampleget> instead of using dynamic, then you can make use of an additional property which does conversion to Exampleget. But this comes at a cost of additional serialization/deserialization, hence wouldn't recommend unless you have good reasons to stick to it.

public class GetNumbers
{
    [JsonExtensionData]
    public Dictionary<string,object> Values;
    [JsonIgnore]
    public Dictionary<string,Exampleget> ExamplegetDictionary=> Values.Select(x=>new KeyValuePair<string,Exampleget>(x.Key, ((object)x.Value).Cast<Exampleget>()))
                                                                      .ToDictionary(x=>x.Key,y=>y.Value);

}

public static class Extension
{
    public static T Cast<T>(this object value)
    {
        var jsonData = JsonConvert.SerializeObject(value);
        return JsonConvert.DeserializeObject<T>(jsonData);
    }
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • We need to add any plugins..I was using JsonUtility for unity.I was trying to solve using Jsonutility.What is ' T ' Cast ..I did not understand that...Please chk......https://stackoverflow.com/questions/53882500/argumentoutofrangeexceptioncant-figure-out-error-while-working-with-poco-class?noredirect=1#comment94707010_53882500 – zyonneo Dec 27 '18 at 10:24
  • The above code Newtonsoft.Json for deserialization. The Cast method is used to cast a dynamic object to T(in this case, Exampleget). – Anu Viswan Dec 27 '18 at 10:35