Unity 中的 JsonUtility 不再工作。为什么?

我正在尝试从服务器获取的 Json 中获取整数值。我使用了 Unity 库中的 JsonUtility,它运行良好。突然间它不再解析了。所有返回值均为 Null。


//SAMPLE CODE

SpinResult res = JsonUtility.FromJson<SpinResult>(download.downloadHandler.text);

spinValue = res.result;

//spinValue is always 0. It was working fine



//CLASS

[System.Serializable]

public class SpinResult

{

    public int result;

}



//JSON

{

    "data": {

        "type": "",

        "id": "",

        "attributes": {

            "server_seed": "",

            "client_seed": "",

            "result": 31,

        },

        "next_spin": {

            "hashed_server_seed": "",

            "client_seed": ""

        }

    }

}

我只需要整数“RESULT”,在本例中它应该是 31,但实际输出始终为 0。我每次都会检查 Json,它工作得很好。


小怪兽爱吃肉
浏览 94回答 1
1回答

至尊宝的传说

我还没有测试过这个,但尝试让你的类看起来像这样:[System.Serializable]public class SpinResult{&nbsp; &nbsp; public string type;&nbsp; &nbsp; public string id;&nbsp; &nbsp; public Attributes attributes;}[System.Serializable]public class Attributes{&nbsp; &nbsp; public string server_seed;&nbsp; &nbsp; public string client_seed;&nbsp; &nbsp; public int result;}然后要获取您的价值,result请使用:int spinValue = res.attributes.result;
打开App,查看更多内容
随时随地看视频慕课网APP