2

Here is my JSON file:

[
  {
    "name": "An item"
  }
 ]

Here is my helper class:

public static class JsonHelper
    {
        public static T[] FromJson<T>(string json)
        {
            string newJson = "{ \"array\": " + json + "}";
            Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
            return wrapper.array;
        }

        [System.Serializable]
        private class Wrapper<T>
        {
            public T[] array;
        }
    }

Here is the class I am trying to make objects out of:

public class Item
{   
    public string name, desc;
}

This is where I am calling the helper class:

Item[] itemList = JsonHelper.FromJson<Item>(itemJson.text);

The problem is itemList and wrapper.array in JsonHelper is null.

I copied code directly from:

http://www.boxheadproductions.com.au/deserializing-top-level-arrays-in-json-with-unity/

and

https://forum.unity.com/threads/how-to-load-an-array-with-jsonutility.375735/

What am I doing wrong?

Hyrial
  • 1,728
  • 3
  • 15
  • 27
  • Unity's JSON implementation doesn't support unwrapped top-level arrays. You don't want to get into the habit of using them anyways, considering top-level JSON arrays have been the target of several security exploits over the years. – arkon Sep 23 '19 at 04:22

1 Answers1

2

I suggest you install json.net NuGet package for Unity by using below command in package manager console

Install-Package Unity.Newtonsoft.Json -Version 7.0.0

And then without modifying much of your code, you can get it to work

Just replace your JsonHelper class with below

public static class JsonHelper
{
    public static T[] FromJson<T>(string json)
    {
        string newJson = "{ \"array\": " + json + "}";
        JToken jToken = JToken.Parse(newJson);
        Wrapper<T> wrapper = jToken.ToObject<Wrapper<T>>();
        return wrapper.array;
    }

    [System.Serializable]
    private class Wrapper<T>
    {
        public T[] array;
    }
}

And you need to add using Newtonsoft.Json.Linq; namespace to your program.

Output: (From Debugger)

enter image description here

Caution: using Newtonsoft.Json does not work in WebGL projects.

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • Thanks, it works. But what is wrong with the usage of Unity's official JSON library in my code? – Hyrial Mar 06 '19 at 07:55
  • 3
    I see nothing wrong with your code in your question but the unity Json library is a bit lacking, as is evident by its inability to support top-level arrays. – Lasse V. Karlsen Mar 06 '19 at 07:58
  • @HenryZhang, I think the above comment by **Lasse Vågsæther Karlsen** is right because your json is top-level array. and if my answer was help you then mark the tick on left side of answer to make it green and vote up also :) – er-sho Mar 06 '19 at 09:13
  • There are other things the built-in json utility can't do, either, such as a custom converter so you can serialize things in a non-standard way. Say, for example, in Minecraft I want to save out the world as blocks. I don't want to encode the entire `Block` class at each position! I want to encode merely a *reference* that I can look up during deserialization. Say, `"block_stone"` or `1`. – Draco18s no longer trusts SE Mar 06 '19 at 15:47
  • A word of caution - using Newtonsoft.Json does not work in WebGL projects. – Vixxd May 07 '19 at 16:37
  • You don't need to use an external library just for a wrapper... http://www.boxheadproductions.com.au/deserializing-top-level-arrays-in-json-with-unity/ – arkon Sep 23 '19 at 06:44
  • this does not work if you have JSON that contains Vector3 and such `ArgumentException: Could not cast or convert from System.String to UnityEngine.Vector3.` – ina Nov 04 '20 at 06:31