Biribu
Biribu

Reputation: 3823

SimpleJson in WP8

I am trying to parse a json file in WP8. For the moment I just need to get a list of topics with some titles each one. Something like:

[
{"topic":"topic1", 
 "titles":[{"title":"tit1"},
           {"title":"tit2"},
           {"title":"tit3"}]},
{"topic":"topic1",
 "titles":[{"title":"tit1"},
           {"title":"tit2"},
           {"title":"tit3"}]}
]

My idea is get each topic and save in an array of 2 dimensions. In topic[X][0] would go topic and topic[x][y] the titles...

I have found this topic: Deserializing JSON using JSon.NET with dynamic data

In which is explained a bit how to do it but I am not able to get any of my data because the structure of the json is not similar. Any idea of how to do in this case?

Upvotes: 0

Views: 45

Answers (1)

Jonathan Camilleri
Jonathan Camilleri

Reputation: 631

To parse just call:

JArray json = JsonConvert.DeserializeObject(jsonText) as JArray;

For getting the topics just access it normally:

JObject arrayItem = json[0] as JObject;

Getting the topic and it's value:

JValue topic = arrayItem["topic"] as JValue;
string topicValue = topic.Value.ToString();

Getting the titles:

JArray titles = ArrayItem["titles"] as JArray;

And getting their values:

foreach (JObject jo in titles)
{
    JValue title = jo["title"] as JValue;
    string titleValue = title.Value.ToString();
}

Upvotes: 1

Related Questions