1

When i am trying to deserialize the Json string using JsonFx , I receive the value null.

Here is my Json String

{
  "PlayerID": 123,
  "PlayerName": "Avi",
  "DemiGods": {
    "Red": {
      "Name": "Red",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Blue": {
      "Name": "Blue",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Green": {
      "Name": "Green",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Light": {
      "Name": "Light",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Dark": {
      "Name": "Dark",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    }
  },
  "PlayerGrid": {
    "Red": {
      "x": 0,
      "y": 1
    },
    "Blue": {
      "x": 1,
      "y": 1
    },
    "Green": {
      "x": 2,
      "y": 1
    },
    "Light": {
      "x": 2,
      "y": 2
    },
    "Dark": {
      "x": 3,
      "y": 2
    }
  },
  "AIGrid": {
    "Red": {
      "x": 0,
      "y": 1
    },
    "Blue": {
      "x": 1,
      "y": 1
    },
    "Green": {
      "x": 2,
      "y": 1
    },
    "Light": {
      "x": 2,
      "y": 2
    },
    "Dark": 
    {
      "x": 3,
      "y": 2
    }
  }
}

This is my class where i stores the data from Json

public class UnitsInfo :MonoBehaviour
{
    public string PlayerName;
    public int PlayerID;
    public List<DemiGods> demigodslist = new List<DemiGods>();
    public List<GridData> playerGridlist = new List<GridData>();
    public List<GridData> AIGridList = new List<GridData>();

    public UnitsInfo() 
    {
        Debug.Log("Default Constructor");
    }

    public UnitsInfo(string _name, int id, List<DemiGods> Godlist, List<GridData> plist, List<GridData> AIlist)
    {
        PlayerName = _name;
        PlayerID = id;
        demigodslist = Godlist;
        playerGridlist = plist;
        AIGridList = AIlist;
    }

    public class DemiGods
    {
        public string Name;
        public int Level;
        public float Attack;
        public float Dodge;
        public float Defence;
        public float Speed;
        public DemiGods() 
        {
            Debug.Log("DemIGOds DeFALUT ConsTruCtoR");
        }
        public DemiGods(string _name, int _lvl, float _attack, float _dodge, float _Defence, float _speed)
        {
            Name = _name;
            Level = _lvl;
            Attack = _attack;
            Dodge = _dodge;
            Defence = _Defence;
            Speed = _speed;
        }
    }

    public class GridData
    {
        public Vector2 pos;

        public GridData() { Debug.Log("Grid DAta DeFALUT ConsTruCtoR"); }
        public GridData(int x, int y)
        {
            pos.x = x;
            pos.y = y;
        }
    }
  }

This is where is deserialize data 
public class JsonData: MonoBehaviour 
{
    public string JSONString;
    void Start()
    {
        UnitsInfo HerosList = JsonReader.Deserialize<UnitsInfo>(JSONString);
        Debug.Log(HerosList);
    }
}

The Debug.log return null. Where im going wrong??

Thanks in Advance
scott_lotus
  • 3,171
  • 22
  • 51
  • 69
avisingh
  • 121
  • 1
  • 8

1 Answers1

1

I think your json does not match what you are after. In your case I would think you expect Red, Blue, Green to be DemiGods object where Red, Blue, Green is their name.

But the way the json does at the moment, it expects Red, Blue, Green to be type.

    {
    "PlayerID": 123,
    "PlayerName": "Avi",
    "DemiGods": [{
        "Name": "Red",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Blue",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Green",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Light",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Dark",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }]
}

In the above, DemiGods is an array of objects of type DemiGods, the name is contained inside the object. So you would have to declare a method to grab either iterating through the array until you get an object matching the name or creating a dictionary with string, DemiGods.

And the same applies for the rest of the Json file obviously.

EDIT:

Here is your valid json (at least i think)

{
    "PlayerID": 123,
    "PlayerName": "Avi",
    "DemiGods": [{
        "Name": "Red",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Blue",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Green",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Light",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Dark",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }],
    "PlayerGrid": [{
        "x": 0,
        "y": 1
    }, {
        "x": 1,
        "y": 1
    }, {
        "x": 2,
        "y": 1
    }, {
        "x": 2,
        "y": 2
    }, {
        "x": 3,
        "y": 2
    }],
    "AIGrid": [{
        "x": 0,
        "y": 1
    }, {
        "x": 1,
        "y": 1
    }, {
        "x": 2,
        "y": 1
    }, {
        "x": 2,
        "y": 2
    }, {
        "x": 3,
        "y": 2
    }]
}

And here is the csharp:

public class DemiGod
{
    public string Name { get; set; }
    public int Level { get; set; }
    public int Attack { get; set; }
    public int Dodge { get; set; }
    public int Defence { get; set; }
    public int Speed { get; set; }
}

public class PlayerGrid
{
    public int x { get; set; }
    public int y { get; set; }
}

public class AIGrid
{
    public int x { get; set; }
    public int y { get; set; }
}

public class RootObject
{
    public int PlayerID { get; set; }
    public string PlayerName { get; set; }
    public List<DemiGod> DemiGods { get; set; }
    public List<PlayerGrid> PlayerGrid { get; set; }
    public List<AIGrid> AIGrid { get; set; }
}

I do not exactly how JsonFX works but I would guess something like:

public class JsonData:MonoBehaviour{

    public RootObject root;
    void Start(){   
         root = JsonFX.Deserialize<RootObject>(jsonFile);
    }
}
Everts
  • 10,408
  • 2
  • 34
  • 45
  • So untill my class is not in match with my Json, it is going to throw Null?? and if i want deserialize an Object inside my Object then how can do that ?? Any leads or tiny example will be much appriciated – avisingh Feb 10 '16 at 11:18
  • Of course, the serializer can only convert objects into known objects. You want a list of DemiGods, so you need to have a json that corresponds to that. You need to think your json so that it fits what you are after. In your case, the c# side makes sense. Rewrite the json to use the array as I showed in the answer and that should do. – Everts Feb 10 '16 at 11:22
  • Try to paste your json in json2csharp.com and see the result. It is not so intuitive and easy to maintain compare to what you already have. – Everts Feb 10 '16 at 11:23
  • Yeah it worked.... I converted all the Objects to Arrays and Added type to Each. Now Demigod constructor invoked 5 times and is good to go. PlayerGrid and AIGrid constructors invoked 5 times .. Still the Log Return null..... Modified the Json and pasted in json2sharp. Copied the c# code .... yet it returns null. – avisingh Feb 10 '16 at 12:06
  • Nope... actually it was working. I was using wrong Json string to calculate my Data. Sorry my mistake – avisingh Feb 10 '16 at 12:21