9

This is my sample JSON:

{"data":[{"id":141,"layoutLabel":"Sameer","hasCustomProb":1},
{"id":214,"layoutLabel":"abc","hasCustomProb":0}],"status":200}

This is the class I made

[System.Serializable]
public class PlayerInfo
{
    public string [] data;
    public int status;
}

This is how I get "status" from JSON:

PlayerInfo P = JsonUtility.FromJson<PlayerInfo>(json);
Debug.Log(P.status) //returns 200

Can someone help me out can I get and save the data array or maybe get data.id and data.hasCustomProb? I am new to C# and unity.

2
  • data isn't type string[]. In your sample JSON it is an array of objects that each have an "id","layoutLabel", and "hasCustomProb" properties. Commented Nov 11, 2016 at 9:55
  • Aren't these questions allowed to be asked at gamedev.stackexchange.com ? Commented Mar 13, 2018 at 9:45

1 Answer 1

13

Your class should look like this

[System.Serializable]
public class PlayerInfo
{
    public List<ActData> data;
    public int status;
}

[System.Serializable]
public class ActData
{
    public int id;
    public string layoutLabel;
    public int hasCustomProb;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Except that you have to remove { get; set; } from all of the variables. JsonUtility cannot work with that.
@Programmer you are right. I wanted to sure if Serializable attribute is required as i just edited this answer?
Yup, that too or OP will get empty result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.