Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing JSON using JSon.NET with dynamic data
    primarykey
    data
    text
    <p>I'm trying to deserialize some JSON data into objects for an application. Up until now it's been fine because the properties on the JSON data was static (key with a value). Now I've got a result where the key is a dynamic piece of data.</p> <p>Here's an example JSON url:</p> <p><a href="http://en.wikipedia.org/w/api.php?action=query&amp;format=json&amp;pageids=6695&amp;prop=info">http://en.wikipedia.org/w/api.php?action=query&amp;format=json&amp;pageids=6695&amp;prop=info</a></p> <p>The resulting JSON for this is:</p> <pre><code>{ "query" : { "pages" : { "6695" : { "counter" : "", "lastrevid" : 468683764, "length" : 8899, "ns" : 0, "pageid" : 6695, "title" : "Citadel", "touched" : "2012-01-03T19:16:16Z" } } } } </code></pre> <p>Okay, that's great except I can't deserialize the "pages" data into an object. If I were to define a class for the pages it would have to look like this:</p> <pre><code>public class 6695 { public string counter { get; set; } public int lastrevid { get; set; } public int length { get; set; } public int ns { get; set; } public int pageid { get; set; } public string title { get; set; } public string touched { get; set; } } </code></pre> <p>In order to deserialze the contents (using JsonConvert.Deserialize(jsondata)) and we all know we can't have a class called 6695. Not only that, the name of the class would have to be different (for example pageid=7145 would have to be the 7145 class).</p> <p>I can seem to pluck some values out if I use something like JObject.Parse(content) and then access items as JArrays but it's pretty ugly and I'm still stuck on trying to get out the data from the pages array.</p> <p>Looking for someone to help with this. I don't think it's uncommon, it's just not JSON data I've come across before and not sure how to handle it.</p> <p>Thanks!</p> <p>PS forgot to mention this is on Windows Phone 7 so "dynamic" isn't available!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload