Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 xKroniK13x · Mar 29, 2013 at 06:35 PM · arraysserializationjson

MiniJSON Multiple Rows

Hello,

I am using the MiniJSON Lib found here. I am using UnityScript, not C#, but that isn't a big deal, I followed this example for that. The demo works perfectly, however, I have more than one row of JSON. An example string of the text is... [{"name":"scene name","sceneID":"1"},{"name":"scene name two","sceneID":"2"}] However, I cannot pull from the second, "scene name two." I was curious if anyone knew the syntax to do this... it is similar to the Object variable in their example, but they don't show how to get that information.

Any help would be great! Thanks!

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 hoy_smallfry · Mar 29, 2013 at 07:49 PM 0
Share

Can you show us the code you are using and perhaps the full structure of the JSON file?

avatar image xKroniK13x · Mar 29, 2013 at 07:59 PM 0
Share

Sure thing. That is the exact structure of the JSON file, it's very basic.

This is the script I am using.

 //Create PHP Post Form
 var formList = new WWWForm();
 formList.AddField("pid", userID);
 
 //POST the information
 var list_post = WWW(sceneListUrl,formList);
 yield list_post; //Yield until completed
 
 //Could not connect
 if(list_post.error) {
     print("There was an error listing: " + list_post.error);
    
 //Connection Successful
 } else { 
     print(list_post.text);
     var dict = Json.Deserialize(list_post.text) as Dictionary.<String,System.Object>;
 Debug.Log("dict['name']: " + dict["name"] as String);    
 }
 listDone = true;

I have also tried using dict["name"][0] or dict[0]["name"] and get null reference exceptions or an error saying it doesn't support slicing.

avatar image hoy_smallfry · Mar 29, 2013 at 10:01 PM 0
Share

Ah, I see what your problem is.

1 Reply

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

Answer by hoy_smallfry · Mar 29, 2013 at 10:13 PM

You are trying to cast to a dictionary, but the first structure you provide in the JSON does not translate to a dictionary.

All vars derive from System.Object. If you are not familiar with that concept, it means that even though a variable may be a String, it is also a System.Object, sort of in the same way that an apple and a banana are both fruit and therefore have all the characteristics of fruit.

Example of how this can be useful: System.Object has a GetType() function, therefore all objects have that function, which will tell you what the real type is:

 var obj : System.Object = "Hello";
 var type = obj.GetType(); // will give us the real type.
 print(type); //should say System.String

 obj = 12;
 print(obj.GetType()); // now its different

Now, with MiniJson:

  • Anything that is wrapped in curly brackets {} converts to Dictionary. All the keys are the strings on the left side of colon :, and all the values are on right side of it.

  • Anything wrapped in square brackets [] converts to List.

  • Any values converts to System.String, which then can be converted to whatever type you want with System.Convert functions

Since the outermost brackets in your JSON file are square brackets, that means the object deserialized is a List:

 System.Object obj = Json.Deserialize(list_post.text);

 print(obj.GetType()); // should say List`1<System.Object> or ArrayList, depending on your version of MiniJson

 var list = Json.Deserialize(list_post.text) as List.<System.Object>;

Then, since all the array elements are wrapped in curlies, they become dictionaries:

 print(list[0].GetType()); // Dictionary`1<String, System.Object> or HashTable

 var scene = list[0] as Dictionary.<String, System.Object>;

 print(scene["name"]);
 print(scene["sceneID"]);

Hope that helps!

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 xKroniK13x · Mar 30, 2013 at 05:09 AM 0
Share

Absolutely perfect. Thank you so much for explaining that all to me, it makes much more sense! It helped me with this project and also my understanding of JSON. :)

avatar image PooBear · Jun 23, 2016 at 10:55 AM 0
Share

Brilliant, spent a long time looking for an explanation like this.

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

12 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

Related Questions

Json wrapped array deserialization 1 Answer

Serializing class that has List of members of same type and referencing them in scene causes awfull long assembly reload time 0 Answers

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

What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 Answers

UnityScript for unique simpleJSON lists 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