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
1
Question by Scorr · Dec 28, 2015 at 02:13 AM · serialization

JsonUtility nested array deserialization

Is it possible to deserialize nested arrays from json using JsonUtility.FromJson() from 5.3?

Example:

 {
   "test": [[0, 1], [2, 3], [4, 5]]
 }

Should output into a class with:

 int[][] test;

(If I'm not mistaken. This question is about the first example either way.)

Comment
Add comment · Show 3
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 gjf · Dec 27, 2015 at 07:36 PM 0
Share

from the docs: "Any plain class or structure is supported, as well as classes derived from $$anonymous$$onoBehaviour or ScriptableObject."

what (code) did you try?

avatar image Scorr gjf · Dec 27, 2015 at 07:55 PM 0
Share

I've tried int[][] and int[,] with the above example json, which doesn't seem to work. I've also tried using a seperate class:

 public IntArray[] test;
 
 public class IntArray {
     public int[] vector2;
 }

This DOES work, however only if the json structure is as:

 "test": [ { "vector2": [0, 1] }, { "vector2": [2, 3] } ]

I'm trying to store vector2 coordinates and though this version does work, the first json example seems a lot cleaner and readable to me, but I don't know how to get that result.

avatar image gjf Scorr · Dec 27, 2015 at 08:54 PM 0
Share

i just tried the following which didn't even create the expected JSON object.

 using UnityEngine;
 
 public class JSONTest : $$anonymous$$onoBehaviour
 {
     public class JSONTestClass
     {
         public int[,] TestArray;
         public int TestInt;
         public string TestString;
     }
 
     public void Start()
     {
         var testClass = new JSONTestClass
             {
                 TestArray = new [,] {{0,0,0,0},{1,1,1,1},{2,2,2,2},{3,3,3,3}},
                 TestInt = 64,
                 TestString = "testString"
             };
 
         Debug.Log(JsonUtility.ToJson(testClass));
     }
 }

3 Replies

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

Answer by superpig · Jan 06, 2016 at 02:39 AM

Unfortunately, the Unity serializer itself does not support the kind of nested array structure you are trying to use, which means that JsonUtility (which is built on the Unity serialiser) does not support it either.

It's possible this will come in a future update but for now the only workaround is to do something like the IntArray setup you already saw. Alternatively, have you tried just using a Vector2[] ?

Comment
Add comment · Show 5 · 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 c5hopper · Dec 01, 2016 at 06:45 PM 0
Share

Hi superpig, do you know if there is any workaround for this? I have JSON that contains nested arrays that is co$$anonymous$$g from another system, so unfortunately don't have the option to changed the structure as suggested above. Thanks!

avatar image Bunny83 c5hopper · Dec 01, 2016 at 07:05 PM 0
Share

Yes, simply don't use Unity's JsonUtility. It has the same restrictions as Unity's serialization system. You can use any other Json solution. For example my SimpleJSON parser. It doesn't deserialize to C# classes but to a tree structure of JSONNodes so you can easily access the data. Just look at the examples on the wiki page.

There are other solutions as well, just look around the net.

avatar image c5hopper Bunny83 · Dec 02, 2016 at 06:48 AM 0
Share

Thanks @Bunny83. I quite like the idea of deserializing straight into C# classes, though. Is there not a way to workaround/handroll the deserialization of the array of arrays? I came across some approaches using ISerializationCallbackReceiver, though I couldn't figure out if/how that would work for this case.

Show more comments
avatar image
0

Answer by atharahmad · Oct 28, 2016 at 01:05 PM

using UnityEngine; using System.Collections.Generic;

public static class JsonParser { public static List FromJsonArray(string json) { List objects = new List (); // start with { ends with }

     int start = json.IndexOf ('['); // where array starts
     int length;
     while ((length = json.IndexOf ("},{", start)) != -1) {  // how many objects in json
         objects.Add (json.Substring (start + 1, length - start));  // cutting string by finding { and }
         start = length + 1;
         //Debug.Log (objects [objects.Count - 1]);
     }
     length = json.IndexOf ("}", start);
     objects.Add (json.Substring (start + 1, length - start)); // last object left 
     //Debug.Log (objects [objects.Count - 1]);

     List <T> list = new List <T> ();
     foreach (string obj in objects)
         list.Add (JsonUtility.FromJson <T> (obj));
     return list;
 }

} this works fine for this json... [{"id":"wdawdaw","name":"bin","level":"4","subscriptions":"24"},{"id":"wdadsdwda","name":"2daw","level":"2","subscriptions":"3"},{"id":"wdadsdwdasdaw","name":"vas","level":"2","subscriptions":"3"}]

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
0

Answer by ElectricMonk · Feb 24, 2017 at 09:00 PM

A good example of how to handle this on Stack Overflow here: http://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity

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

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

7 People are following this question.

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

Related Questions

Saving players progress in ScriptableObject asset 1 Answer

Save slider value at runtime, and reload latest value on launch 1 Answer

Get Asset at runtime by its ID 0 Answers

Save and load serialization problem 0 Answers

Serialize Data - After Update 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