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

标签 c# json unity-game-engine

这是我的示例 JSON:

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

这是我做的类(class)

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

这就是我从 JSON 获取“状态”的方式:

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

有人可以帮助我获取并保存数据数组或者获取 data.id 和 data.hasCustomProb 吗?我是 C# 和 unity 的新手。

最佳答案

你的类应该是这样的

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

关于c# - 在Unity中使用JsonUtility.FromJson反序列化JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40545315/

相关文章:

android - 如何使用 Android Studio 将第三方库添加到 unity android build

c# - 在 headless (headless)模式下独立运行 Unity,同时捕获屏幕截图

c# - 如何检查在模拟对象上使用特定参数调用的方法?

c# - foreach 循环是否正确处理列表长度的变化?

javascript - Ajax 和 Php 帖子问题

json - JSON 名称-值对可以有一个空名称吗?

c# - 通过 wpf 中的内容呈现器将公共(public)字符串属性绑定(bind)到 TextBox 文本

c# - Entity Framework - 数据库和模型列/类型不匹配 - 映射选项

java - 如何在android中使用GSON创建JSONArray

c# - UnityEvent 未在检查器中显示动态方法调用(其他 Unity 事件也部分损坏)