Get JSON value with randomly generated value

I have a JSON file that looks like this. This is basically a JSON file taken from Wikipedia using its API.

{
    "batchcomplete": "",
    "query": {
        "pages": {
          "31329803": {
            "pageid": 31329803,
            "ns": 0,
            "title": "Wiki Title",
            "extract": "<p><b>Your Wiki Title</b></p>"
          }
        }
    }
}

The number generated in the "pages" (which is the identifier of the pageID) is random. I am trying to extract the value of "extract" , but I cannot get it.

I am using Visual Studio and using NewtonSoft JSON.net for parsing. I created a class to retrieve the data I want and it looks like this.

    public class WikiPage
    {
        public string title { get; set; }
        public int pageid { get; set; }
        public int ns { get; set; }
        public string extract { get; set; }
    }

I am trying to get around the JSON tree and get the value I want. The code I use to get the value is as follows:

static void Main(string[] args)
{
   // Getting JSON string from file            
   string JSONString = File.ReadAllText("wiki.json");

   JObject wikiSearchResult = JObject.Parse(JSONString);
   IList<JToken> wikiPages = wikiSearchResult["query"]["pages"].Children().ToList();

   JToken result = wikiPages[0];
   var wp = JsonConvert.DeserializeObject<WikiPage>(result.ToString());

   // Writing data
   Console.WriteLine(wp.extract);
   Console.ReadLine();
}

When I run the program, I get an error message:

"Newtonsoft.Json.JsonSerializationException" Newtonsoft.Json.dll

: "31329803" 'JSON_test.WikiPage. '', 1, 10.

, . , , , - ?

+4
2

, result :

JToken result = wikiPages[0].Children().First();
0

. :

var json = JObject.Parse(JSONString);
var pages = json["query"]["pages"].ToObject<Dictionary<string, WikiPage>>();

var page = pages.First().Value;
Console.WriteLine(page.extract);
0

All Articles