简体   繁体   中英

JsonUtility in Unity is not working anymore. Why?

I'm trying to get a integer value from a Json that I GET from a server. I used JsonUtility from Unity libreries and It was working fine. And suddenly it is not parsing anymore. All values returned are 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": ""
        }
    }
}

I just need the integer "RESULT", in this case it should be 31 but the actual output is always 0. I check the Json everytime and its working perfectly fine.

I have not tested this, but try making your class look like this:

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

Then to get the value of result you would use:

int spinValue = res.attributes.result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM