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

时间:2019-08-26 15:53:37

标签: c# json parsing unity3d json.net

我正在尝试从服务器获取的Json中获取整数值。我使用了来自Unity libreries的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,并且它的工作情况很好。

1 个答案:

答案 0 :(得分:0)

我尚未对此进行测试,但请尝试使您的班级看起来像这样:

[System.Serializable]
public class SpinResult
{
    public string type;
    public string id;
    public Attributes attributes;
}

[System.Serializable]
public class Attributes
{
    public string server_seed;
    public string client_seed;
    public int result;
}

然后使用以下值来获取result的值:

int spinValue = res.attributes.result;