8

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.
    – Lithium
    Nov 11, 2016 at 9:55
  • Aren't these questions allowed to be asked at gamedev.stackexchange.com ? Mar 13, 2018 at 9:45

1 Answer 1

12

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;
}
3
  • 6
    Except that you have to remove { get; set; } from all of the variables. JsonUtility cannot work with that.
    – Programmer
    Nov 11, 2016 at 10:29
  • 2
    @Programmer you are right. I wanted to sure if Serializable attribute is required as i just edited this answer?
    – Umair M
    Nov 11, 2016 at 11:06
  • Yup, that too or OP will get empty result.
    – Programmer
    Nov 11, 2016 at 11:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.