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?

1
  • 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. Commented Sep 23, 2019 at 4:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, it works. But what is wrong with the usage of Unity's official JSON library in my code?
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.
@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 :)
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.
A word of caution - using Newtonsoft.Json does not work in WebGL projects.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.