0

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?

Community
  • 1
  • 1
Biribu
  • 3,615
  • 13
  • 43
  • 79
  • You just need `List` of your objects `topic` in which would be `List` of `Title`s. And then you can parse using JSon.NET your json to your object. – RenDishen Jan 21 '15 at 12:54

1 Answers1

1

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();
}
Jonathan Camilleri
  • 611
  • 1
  • 6
  • 17
  • you can go here: http://james.newtonking.com/json/help/index.html however I learnt it by assigning the values to a var and reading the debug information. – Jonathan Camilleri Jan 21 '15 at 15:57