- Home /
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!
Can you show us the code you are using and perhaps the full structure of the JSON file?
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. 
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!
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. :)
Brilliant, spent a long time looking for an explanation like this.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                