简体   繁体   中英

Using JsonUtility.FromJson to deserialize JSON in Unity

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM