MiniJSON Multiple Rows


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!

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>;


Hope that helps!