Suleiman Almaawali
Suleiman Almaawali

Reputation: 31

Using JsonUtility FromJson to deserialize JSON in Unity

I'm having an issue while trying to read a Json string in unity. I created Classes based on the json response im receiving but im not able to deserialize this json Where I did wrong, can anybody help?

{
  "status": 200,
  "isSuccess": true,
  "message": "Suggestion Found",
  "response": {
    "result": [
      {
        "OriginalWord": "goodboy",
        "suggests": [
          {
            "suggestWords": "good boy"
          },
          {
            "suggestWords": "Cordoba"
          },
          {
            "suggestWords": "Catawba"
          },
          {
            "suggestWords": "Catawba's"
          }
        ]
      }
    ]
  }
}

My Classes

[Serializable]
public class Suggest
{
    [SerializeField]
    public string suggestWords { get; set; }
}

[Serializable]
public class Result
{
    [SerializeField]
    public string OriginalWord { get; set; }
    [SerializeField]
    public List<Suggest> suggests { get; set; }
}

[Serializable]
public class Response
{
    [SerializeField]
    public int status { get; set; }
    [SerializeField]
    public bool isSuccess { get; set; }
    [SerializeField]
    public string message { get; set; }
    [SerializeField]
    public List<Result> result { get; set; }
}

Im Deserializing like this

Response response = JsonUtility.FromJson<Response>(jsonString);

Upvotes: 2

Views: 6628

Answers (1)

Blogy Junky
Blogy Junky

Reputation: 26

Above every class there got to be [System.Serializable] this is because UnityEngine has its own implementation of Serializable so you got to indicate its the System's that you want to use rather then Unity's. You also dont need to have [SerializeField] since this is if you want to show the property in Unity's inspection window and since this will not go onto any gameobject you dont need this. You just got to make it public.

Also in the class public class Response if you want json to map correctly you wouldnt use public List<Result> result { get; set; }, it would have to be named response and it will have to be 1 object not a list. So you can create a class called Results and have it have a list variable called result and it will be a list of type Result (no s). and in the result it would have the OriginalWord and a list of Suggest called suggests

Moreover, you must have a constructor for each class for it to work. So it would look like this:

[System.Serializable]
public class Suggest
{
    public string suggestWords;
    public Suggest(string suggestWords)
    {
        this.suggestWords = suggestWords;
    }
}

[System.Serializable]
public class Result
{
    public string OriginalWord;
    public List<Suggest> suggests;

    public Result(string OriginalWord, List<Suggest> suggests)
    {
        this.OriginalWord = OriginalWord;
        this.suggests = suggests;
    }
}

[System.Serializable]
public class Results
{
    public List<Result> result;

    public Results(List<Result> result)
    {
        this.result = result;
    }
}

[System.Serializable]
public class Response
{
    public int status;
    public bool isSuccess;
    public string message;
    public Results response;

    public Response (int status, bool isSuccess, string message, Result response)
    {
        this.status = status;
        this.isSuccess = isSuccess;
        this.message = message;
        this.response = response;
    }
}

Upvotes: 1

Related Questions