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.

Upvotes: 1

Views: 2223

Answers (1)

Casey Crookston
Casey Crookston

Reputation: 13965

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;

Upvotes: 2

Related Questions