Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Arcana96 · Dec 27, 2016 at 03:23 AM · c#scripting problemserializationjson

How do I go about deserializing a json array?

I'm currently making a Task system for my game and I want task data to be stored within a json file like so:

 [
   {
       "name": "Task one",
       "description": "This is task one",
       "type": 0
   },
   {
       "name": "Task two",
       "description": "This is task two",
       "type": 2
   },
   {
       "name": "Task three",
       "description": "This is task three",
       "type": 1
   }
 ]

Although it doesn't seem like Unity has any way of handling json arrays. How do I do this?

Comment
Add comment
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

3 Replies

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

Answer by Arcana96 · Dec 27, 2016 at 05:58 PM

Here's a helper class that does it all for you.

 public class JsonHelper
 {
     public static T[] getJsonArray<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;
     }
 }


Then you can just do something like the following:

YourClass[] yourClass; yourClass = JsonHelper.getJsonArray(Your Json Data);

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 KnightRiderGuy · Dec 27, 2016 at 06:08 PM 0
Share

I realize this is a little off topic but just out of curiosity, is there a document size limit on son files?

avatar image Arcana96 KnightRiderGuy · Dec 28, 2016 at 09:46 PM 0
Share

There's not an inherent size, no. However, some servers limit the amount of Json data that can be sent.

avatar image KnightRiderGuy Arcana96 · Dec 31, 2016 at 02:27 AM 0
Share

Thanks @Arcana96, Reason I ask is because we are using a tyson file for our Voice Recognition inputs, I seem to have reached a point in the document where if I try and add more voice commands the file no longer works.... and that was why I was wondering if there was a size limit on the document itself?

avatar image craftercis · Oct 26, 2018 at 10:33 AM 0
Share

Can you please send your scripts here so i can see how you get the JSON file so.

avatar image
1

Answer by Jeet_9811 · Jan 19, 2018 at 06:46 PM

  1. With SimpleJson In the web call back function we will deserialize the response Json into an array or list.

static void WebCall (string jsonString) {
 Debug.Log ("Response :" + jsonString);

 JSONNode test = JSONNode.Parse (jsonString);

 int count = test.Childs.Count ();

 List<User> userList = new List<User>();

 for (int i = 0; i<test.Childs.Count(); i++) {

     userList[i].name = test[i]["name"].Value;
     userList[i].description = test[i]["name"].Value;
     userList[i].type = test[i]["name”].AsInt;
 }

}

[System.Serializable] public class User {

 public string name;
 public string description;
 public int type;

}

2 And simply with JsonUtility you can do

  static void WebCall(){
        
         string response = "{\"users\": [\ n   {\ n       \ "name\ ": \ "Task one\ ",\ n       \ "description\ ": \ "This is task one\ ",\ n       \     "type\ ": 0\ n   },\ n   {\ n       \ "name\ ": \ "Task two\ ",\ n       \ "description\ ": \ "This is task two\ ",\ n       \ "type\ ": 2\     n   },\ n   {\ n       \ "name\ ": \ "Task three\ ",\ n       \ "description\ ": \ "This is task three\ ",\ n       \ "type\ ": 1\ n   }\ n ]}";
                
             Debug.Log ("Response :" + response);
        
                
     UserList userLst = JsonUtility.FromJson<UserList>(response);
        
        
                
     foreach(User user in userLst){
                    
                    
     Debug.Log(“User data: "+ user.name + " " + user.description + “ “ + user.type.ToString());
                
     }
        
            
         [System.Serializable]
          public class User {
             public string name;
             public string description;
             public int type;
         }
            
             [System.Serializable]
            
             public class UserList {
        
                
                 public User [] users;  //Note: users - should be the key for your array in json string.
            
             }
        













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 CaptainIsolation · Feb 16 at 08:41 PM

If anyone come's here with JSON data that is deserialising but an inner array of objects is not deserialising, the [Serializable] tag on the inner object was all that I had missed:


JSON:

 {
   "Name": "name",
    "InnerObjects":[
      {
       "Value1":"1",
       "Value2":"2"
      },
      {
       "Value1":"3",
       "Value2":"4"
       }
     ]
 }



C# Model:

    public class ObjectFromJSON
     {
         public string Name;
         public InnerObject[] InnerObjects;
     }
 
    [Serializable] //This attribute includes it in the JSONUtility deserialisation
     public class InnerObject
     {
         public string Value1;
         public string Value2;
     }



C# Unity JSONUtility deserialisation

     ObjectFromJSON obj = JsonUtility.FromJson<ObjectFromJSON>(jsonMetadataString);
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

279 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 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 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 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 to serialize parameters from JSON? 1 Answer

Odd change in behaviour after I rearranged project files 0 Answers

How to import a list or array of objects from a json file? 1 Answer

How to protect JSON file game data? 3 Answers

C# class to JSON 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