1

I am receiving a JSON string from an API and I need to deserialize it into something I can use in C# and Unity. I am trying to use Unity's JSON Serialization but it doesn't seem to be working for me?

Here is an example of the JSON structure from the API:

{
    "results": [
        [
            "2016-09-01/20160901_200002_000.jpg",
            "2016-09-01/20160901_192851_000.jpg",
            "2016-09-01/20160901_193443_000.jpg",
            "2016-09-01/20160901_210130_000.jpg"
        ],
        [
            "2016-09-02/20160902_104409_000.jpg",
            "2016-09-01/20160901_165949_000.jpg"
        ],
        [
            "2016-09-02/20160902_104409_000.jpg",
            "2016-09-02/20160902_104721_000.jpg",
            "2016-09-02/20160902_093420_000.jpg",
        ],
        [
            "2016-09-02/20160902_082554_000.jpg"
        ]
    ]
}

This is my code trying to deserialize the JSON into a custom class using Unity's JsonUtility class:

[Serializable]
public class MyClass
{
    public List<List<string>> results;
}

MyClass test = JsonUtility.FromJson<MyClass>(jsonString);
Debug.Log(test.results); // this is just returning null 

Anyone any idea what I'm doing wrong?

EDIT: This answer seems to suggest it is because JsonUtility does not support arrays and provides a helper class but it isn't working for this issue.

aadu
  • 3,196
  • 9
  • 39
  • 62
  • 1
    FYI you're trying to ___deserialize___ the JSON – phuzi Mar 25 '20 at 12:00
  • 1
    Does this answer your question? [Serialize and Deserialize Json and Json Array in Unity](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity) – Ian Kemp Mar 25 '20 at 12:03
  • Question I linked as a dupe target has your answer, although it's a long read. I think you may be falling foul of the issue "If you get Null, make sure that the Json is not a Json array. If it is, use the helper class above with JsonHelper.FromJson instead of JsonUtility.FromJson." – Ian Kemp Mar 25 '20 at 12:04
  • Some ideas here: [JsonUtility fails to convert 2D arrays to json and also from json](https://forum.unity.com/threads/jsonutility-fails-to-convert-2d-arrays-to-json-and-also-from-json.387884/) – Iggy Mar 25 '20 at 12:21
  • @IanKemp That is a very helpful answer to that question, however all it does for my question is suggest a reason why the issue is occurring, namely "Unity's JsonUtility does not support arrays as it is still new". And unfortunately the helper class provided is not working for my issue, possibly because I think I have an array inside an array in my JSON so there is an extra layer of complexity? Either way I'm not going to make a new question on how to edit the helper class so this question remains unanswered. – aadu Mar 25 '20 at 12:24

1 Answers1

1

Here is the method I use to decode JSONs:

  1. Manage your NuGets and add Json.NET.Web to your references
  2. Copy paste the following method
  3. Make sure you are getting the right object. It looks like you have one key (="results") and whats comming next is the value corresponding to your key. You can eighter decode "results" and first remove all your brackets "[]" and then split the string by char[','] or form your JSON (if possible) with a key for each value and decode those values.

I hope it helps

public Dictionary<string, string> Decode(string encodedJson, string[] keys)
    {
        var details = JObject.Parse(encodedJson);
        Dictionary<string, string> decodedjson = new Dictionary<string, string>();
        foreach (var key in keys)
        {
            decodedjson.Add(key, details[key].ToString());
        }

        return decodedjson;
    }
Adrian Efford
  • 473
  • 3
  • 8
  • Since this is much easier than trying to hack Unity's JsonUtility class, which I think would be necessary for my answer, I will mark this as the correct solution. – aadu Mar 25 '20 at 12:34
  • Please don't do this @AzzyDude. This "JSON.NET.Web" package was last updated in 2016, has no description, links to a nonexistent GitHub repo, and basically looks dodgy all around. If you are going to use a third-party JSON serialize/deserialize package, use the de facto standard Newtonsoft.Json. – Ian Kemp Mar 25 '20 at 13:23
  • @IanKemp If you would like to submit a similar style answer with Newtonsoft.Json instead I would be happy to set that as the solution. – aadu Mar 25 '20 at 14:50