Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by Xarbrough · Jan 07, 2016 at 03:55 PM · c#serializationjson

JsonUtility array not supported?

I'm reading the documentation on JsonUtility.FromJson and find it hard to believe, that I can't serialize or deserialize an array of serializable objects. This works fine with plugins like LitJson, I was using before, so now I thought it was great, that Unity has json support built-in. Am I doing something wrong or is this maybe a feature to come in the future?

     // Create some already tested dummy data.
     User user = new User();
     user.DisplayName = "TestName";
 
     // Works fine with single object
     string json = JsonUtility.ToJson(user);
     Debug.Log(json); // returns valid json string.
     Debug.Log(JsonUtility.FromJson<User>(json)); // works as it should.
 
     // Doesn't work in array.
     User[] usersArray = new User[3] {user,user,user};
     string json2 = JsonUtility.ToJson(usersArray);
     Debug.Log(json2); // returns "{}"
     Debug.Log(JsonUtility.FromJson<User>(json2)); // is empty.

Thanks! :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hippogames · Dec 13, 2016 at 11:43 AM 0
Share

The same issue with Unity 5.5

6 Replies

· Add your reply
  • Sort: 
avatar image
15
Best Answer

Answer by superpig · Jan 12, 2016 at 02:53 PM

Serializing/deserializing arrays and lists as top-level elements is not supported right now. It's on the to-do list...

Note that you can work around it in two ways for now:

  1. Use ToJson() for each element in the array separately, and just stitch them together with "[", "," and "]"

  2. Wrap the array in a structure like this:

    [Serializable] public struct MyObjectArrayWrapper { public MyObject[] objects; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
12

Answer by JanouschScanblue · Jul 12, 2017 at 04:11 PM

Here is my implementation of a JsonUtility wrapper for arrays and lists and also nested arrays and lists.

 void ParseJsonToObject(string json)
 {
     var wrappedjsonArray = JsonUtility.FromJson<MyWrapper>(json);
 }

 [Serializable]
 private class MyWrapper
 {
     public List<MyObject> objects;
 }

 [Serializable]
 private class MyObject
 {
     public int variable1;
     public string variable2;
     public List<MyNestedObject> nestedObjects;
 }

 [Serializable]
 private class MyNestedObject
 {
     public string nestedVariable1;
     public string nestedVariable2;
 }

And the json would look like this

 { "objects": [
     {
         "variable1": "abc123",
         "variable2": "def456",
         "variable3": [
             {
                  "nestedVariable1": "ghi789",
                  "nestedVariable2": "jkl101"
             },
             {
                  "nestedVariable1": "zxy789",
                  "nestedVariable2": "wfg101"
             }
         ]
     },
     {
         "variable1": "3qaghh5",
         "variable2": "34qhah",
         "variable3": [
             {
                  "nestedVariable1": "3qwe54hb",
                  "nestedVariable2": "4ahb4e"
             },
             {
                  "nestedVariable1": "gverazh4",
                  "nestedVariable2": "233ghs"
             }
         ]
     }
 ] }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Spookles · Dec 04, 2017 at 03:10 PM 0
Share

Unity 2017.2, still no array support. Took me a while but found your answer and it's by far the best solution in my opinion. Thanks alot!

avatar image SepM · Dec 01, 2018 at 05:56 AM 0
Share

Works great! I made the mistake of keeping my own nested structures as ScriptableObjects, which was why I was getting instance id's in my JSON. I removed ": ScriptableObject" from my class header and your approach worked perfectly!

avatar image MarconiusDMM · Apr 12, 2019 at 10:34 AM 0
Share

Note that according to my tests, this does not support polymorphism for $$anonymous$$yObject. So if you make a List<$$anonymous$$yObject>, it will only serialize fields declared in $$anonymous$$yObject, but not any child classes it may have; or none at all if $$anonymous$$yObject is abstract.

avatar image
2

Answer by matchsun · Mar 20, 2017 at 02:20 PM

a simple wrapper to support array

 public JsonHelper ()
         {
         }
         //Usage:
         //YouObject[] objects = JsonHelper.getJsonArray<YouObject> (jsonString);
         public static T[] getJsonArray<T>(string json)
         {
             string newJson = "{ \"array\": " + json + "}";
             Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
             return wrapper.array;
         }
         //Usage:
         //string jsonString = JsonHelper.arrayToJson<YouObject>(objects);
         public static string arrayToJson<T>(T[] array)
         {
             Wrapper<T> wrapper = new Wrapper<T> ();
             wrapper.array = array;
             return JsonUtility.ToJson (wrapper);
         }
 
         [Serializable]
         private class Wrapper<T>
         {
             public T[] array;
         }
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew_ES · Jun 17, 2017 at 11:58 PM 0
Share

Reworking the code snippet a bit to get it to compile, but works perfectly. Also works with generic List objects by using ToArray.

 public class JsonHelper
 {
     //Usage:
     //YouObject[] objects = JsonHelper.getJsonArray<YouObject> (jsonString);
     public static T[] getJsonArray<T>(string json)
     {
         string newJson = "{ \"array\": " + json + "}";
         Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
         return wrapper.array;
     }
 
     //Usage:
     //string jsonString = JsonHelper.arrayToJson<YouObject>(objects);
     public static string arrayToJson<T>(T[] array)
     {
         Wrapper<T> wrapper = new Wrapper<T>();
         wrapper.array = array;
         return JsonUtility.ToJson(wrapper);
     }
 
     [System.Serializable]
     private class Wrapper<T>
     {
         public T[] array;
     }
 }
avatar image PLL andrew_ES · Nov 22, 2017 at 10:51 AM 1
Share
 string newJson = "{ \"array\": " + json + "}";
 Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);

Should be simply

 Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);

Correct?

avatar image Bunny83 PLL · Nov 22, 2017 at 12:59 PM 0
Share

Yes, at least to make the two methods the exact opposite of each other. Currently "getJsonArray" expects a json string that only contains an array as top most element and it wraps it in the additional wrapper to be able to parse it. However "arrayToJson" does not remove the additional wrapper "text" from the converted JSON.


So a json string like "[1,2,3]" would come out as an new int[]{1, 2, 3} but "arrayToJson" would produce this from the same array: "{ "array": [1, 2, 3]}" and not [1, 2, 3].


So you suggest that "getJsonArray" should be modified so it only accepts json in the form { "array": [ .... ]}". Though the better solution would be to actually remove the wrapper object and extract only the array JSON from the returned text. The point of this was to have a transparent "wrapper framework". So it was ment to allow direct conversion of a JSON array to an array and vice versa.

edit
Something like this:

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

 public static string arrayToJson<T>(T[] array)
 {
     Wrapper<T> wrapper = new Wrapper<T> { array = array };
     string json = JsonUtility.ToJson(wrapper);
     var pos = json.IndexOf(":");
     json = json.Substring(pos+1); // cut away "{ \"array\":"
     pos = json.LastIndexOf('}');
     json = json.Substring(0, pos-1); // cut away "}" at the end
     return json;
 }


So this will actually return a JSON array

 string json = JsonHelper.arrayToJson(new int[]{1, 2, 3, 42});
 // --> "[1, 2, 3, 42]"
 // and the reverse:
 int[] numbers = JsonHelper.getJsonArray(json);
Show more comments
avatar image
2

Answer by Pablomon · Feb 01, 2021 at 03:35 PM

It is now the future, year 2021, a big pandemic has striken the whole planet. And seems like we still can't serialize Arrays as top-level elements without a wrapper. Has this issue been forgotten completely or I am missing something?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by flashmandv · Jul 03, 2016 at 09:11 PM

I just tested it in Unity 5.3.5 declaring the field like this:

public Obbj[] obbs = new Obbj[] { new Obbj(), new Obbj() };

And it works :) It seems they added the array support

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaDarkDragon · Sep 15, 2016 at 02:25 AM 2
Share

using 5.4, and i cant seem to get this to work all it outputs is this:

{}

did they remove it? or am i using it wrong?

avatar image LuckyMisterSleven DaDarkDragon · Jan 04, 2017 at 07:35 PM 0
Share

you are probably using setters and getters.

refer to this link http://stackoverflow.com/questions/34274209/why-i-got-empty-json-string-returned-while-using-setter-and-getter-in-the-object

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I go about deserializing a json array? 3 Answers

Serializing subclass of generic class not working 1 Answer

How to protect JSON file game data? 3 Answers

C# class to JSON 1 Answer

Is it possible to executing object serialization into JSON asynchronously? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges