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
0
Question by Thomvdm · Nov 27, 2017 at 03:38 PM · json

Looping through my JSON to spawn objects

I feel like this should be very simple, yet I have a hard time figuring out how to do this.


I'm pulling a JSON string from my server that looks like this:


 [{"coordinates":[[2231,3089]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":36,"l":17}},
 
 {"coordinates":[[2193,3032]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":70,"l":17}},
 
 {"coordinates":[[2241,3009]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":36,"l":17}},
 
 {"coordinates":[[2065,1955]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":112,"l":17}}]


Each element represents a prefab in my game (in this example, 4 times the same one at different coordinates). I want to loop through the JSON and spawn the objects.


So what I'm looking to do is something like this:

 [System.Serializable]
         public class VMGObject
         {
             public string t;
             public static VMGObject CreateFromJSON(string json)
             {
                 return JsonUtility.FromJson<VMGObject>(json);
             }
         }
 private IEnumerator SpawnObjects(string url)
         {
             using (var www = UnityWebRequest.Get(url))
             {
                 yield return www.Send();
                 if (www.isNetworkError || www.isHttpError)
                 {
                     Debug.Log(www.error + " (Error retrieving objects from " + url + ")");
                 }
                 else
                 {
                     var json = new JSONObject(www.downloadHandler.text);
                     var jsonString = "{\"Items\":" + json + "}";
                     VMGObject[] vmgObject = JsonHelper.FromJson<VMGObject>(jsonString);
                     Debug.Log(vmgObject.Length); // 4
                     Debug.Log(vmgObject[0].t); // null

                     // For each element/item in the JSON, instantiate object based on id & coords.
                 }
             }
         }


So the problem here is that it seems like the objects are being created correctly, yet the properties don't get filled (as the length of the array is 4 as it should be, but printing vmgObject[0].t gives null).

What am I doing wrong here? And if this isn't a good way to go, what would be the correct way to iterate over the JSON and retrieve each item/object one by one?

Thanks!

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

1 Reply

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

Answer by Bunny83 · Nov 27, 2017 at 05:04 PM

Well, first of all Unity's JsonUtility class only accepts an object as top most element. You have an array as top most element. So one solution is to wrap your original json in a wrapper object and parse that result.


Next problem could be your nested array. Though as far as i remember it was able to deserialized jagged arrays which the normal Unity serializer doesn't support. So using proper classes to represent your data and wrapping your json in an object should work with the JsonUtility.


[System.Serializable] public class Wrapper { public YourObjectType[] data; }

 public static YourObjectType[] ParseObjects(string json)
 {
     json = "{ \"data\":" + json + "}";
     Wrapper wrapper = JsonUtility.FromJSON<Wrapper>(json);
     return wrapper.data;
 }

Of course "YourObjectType" would be also a serializable class that represents one of your objects and would look something like this:

 [System.Serializable]
 public class YourObjectType
 {
     public double[][] coordinates;
     public int type;
     public TagObject tags;
 }
 
 [System.Serializable]
 public class TagObject
 {
     public string c;
     public string t;
     public string s;
     public double h;
     public double l;
 }

Note that your "tags" object of course can't be "variable". object serialization has to be exact.




However if you want a more dynamic approach you would need to use a generic parser that doesn't serialize the data to fix classes but rather just parses the data into simple structures where you can access your data.


In your code above you use "JSONObject", however it's not clear where that class comes from. I suspect you use this JSONObject? While this framework does certainly work, personally i never used it for several reasons. The parsing is quite inefficient as it calls it's Parse method recursively and copying / duplicating alot of the input string. This is not really a problem for small json files but can become a problem if you have large files / large fields. Also ever JSONObject instance contains fields for all kind of data which makes it quite large. It also doesn't seem to unescape or escape string values.


If those are not a problem for you, you can of course use it. The JSONObject provides an iterator so you can simply use a foreach loop to access each element in your array. The following should work

 var json = new JSONObject(www.downloadHandler.text);    
 foreach(JSONObject o in json)
 {
     JSONObject coord = o["coordinates"][0]; // 0 == first nested array inside coordinates
     float x = coord[0].f // first coordinate
     float y = coord[1].f // second coordinate
     int type = o["type"].i;
     JSONObject tags = o["tags"];
     string id = tags["c"].str;
     // ...
 }




Personally i only use my SimpleJSON framework (it also has a JSONObject class ^^). It uses seperate child classes for the different data types json supports. The parser doesn't generate any unnecessary garbage (only the actual structure). It does properly escape / unescape strings and provides many properies to get easy access to the data and to easily convert certain types. It has implicit conversion operators which makes its use quite transparent. It can be used quite similar to the JSONObject:

 // at the top of the script
 using SimpleJSON;
 
 //
 var json = JSON.Parse(www.downloadHandler.text);
 foreach(JSONNode o in json.Children)
 {
     JSONNode coord = o["coordinates"][0]; // 0 == first nested array inside coordinates
     float x = coord[0] // first coordinate
     float y = coord[1] // second coordinate
     int type = o["type"];
     JSONNode tags = o["tags"];
     string id = tags["c"];
     // ...
 }


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 Bunny83 · Nov 27, 2017 at 05:15 PM 0
Share

I want to add that the version of my SimpleJSON framework that is currently on the wiki / github is not my latest version. I finally stripped out my proprietary binary format into a seperate (partial) class. I've also created a general purpose struct enumerator that doesn't produce any garbage when iterated in a foreach loop.


I've also have some extension methods which allow easy reading of certain Unity types like Vector2/3/4, Rect, Quaternion, ... Those extensions can either use objects or arrays. So a Vector3 could be {"x":5, "y":7, "z":-3} or [5, 7, -3].


Unfortunately this version and the extensions aren't cleaned up and not ready to be published yet. I haven't worked on for the last weeks. If i find the time i might ship the new version.

avatar image Thomvdm · Nov 28, 2017 at 08:18 AM 0
Share

Thank you for the very detailed information! I've switched to using your SimpleJSON, as I still ran into some errors with the other one, and the JSONNode function looks really great to me. Works perfectly! :)

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

Saves on android, could not find file 0 Answers

What is the easiest json library to integrate with Unity? 12 Answers

JSON vs XML for Unity C# 1 Answer

convert Java code that can retrieve the url content to unity javascript 0 Answers

Split string : how to automatically remove " ? 0 Answers


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