Daniel
Daniel

Reputation: 7714

Parsing array in JSON string using MiniJSON

I have this JSON string:

{"person":[{"age":"0","name":"John"}]}

"person" is a list and can contain more people than just John.

Using MiniJSON, how can I read all the people in "person" ?

What I'm trying:

Dictionary<string, object> dict = MiniJSON.Json.Deserialize(jsonString) as Dictionary<string, object>;
List<object> list = (List<object>)(dict["person"]);
Dictionary<string, string> character = list[0] as Dictionary<string, string>;
print(character["name"]);

But it says character["name"] is a null reference.

Upvotes: 1

Views: 706

Answers (1)

Nicolas
Nicolas

Reputation: 479

Deserializing this JSON returns something like this structure (generated via quicktype):

public class Person
{
    public List<PersonElement> PersonList { get; set; }
}

public class PersonElement
{
    public long Age { get; set; }
    public string Name { get; set; }
}

I don't think you can just cast that into a Dictionary<string,object>, hence the null pointer. Try adding a breakpoint and look at the result of the deserialization to see if you can just cast it or if you need to iterate over the result to feed it into a dictionary (if you want to use a dict).

Upvotes: 1

Related Questions