使用JsonUtility.FromJson在Unity中反序列化JSON

本文关键字:反序列化 JSON Unity JsonUtility FromJson 使用 | 更新日期: 2023-09-27 18:17:38

这是我的示例JSON:

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

这是我创建的类

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

这是我如何从JSON中获取"status":

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

有人可以帮助我,我可以得到并保存数据数组或可能得到数据。id和data.hasCustomProb?我是c#和unity的新手。

使用JsonUtility.FromJson在Unity中反序列化JSON

你的类应该是这样的

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