Using JsonUtility.FromJson to deserialize JSON in Unity

谁都会走 提交于 2020-12-29 12:20:29

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/40545315/using-jsonutility-fromjson-to-deserialize-json-in-unity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!