首页 > 其他分享 >Unity内置JSON组件反序列化JSON数据

Unity内置JSON组件反序列化JSON数据

时间:2022-12-25 11:55:32浏览次数:64  
标签:status Unity int System id JSON 序列化 public

Json数据如下:

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

对应的C#解析代码如下:

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

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

此处有几点值得注意:
1) [System.Serializable] 是必须要添加的;
2) 成员变量的访问属性应该为 public ;

Ref: https://qa.1r1g.com/sf/ask/2838172081/

标签:status,Unity,int,System,id,JSON,序列化,public
From: https://www.cnblogs.com/open-coder/p/17003839.html

相关文章