0

The endpoint I'm requesting from:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=eminem

If you look at the JSON inside the Pages Object there's another Object which is the id of the wikipedia article. And in my project, I won't be only parsing Eminem's page but other singers/artists. So my question is how can I go and parse this ever changing json object name?

My json as c# classes

public class ArticleRootobject {
    public string batchcomplete { get; set; }
    public Query query { get; set; }
}

public class Query {
    public Normalized[] normalized { get; set; }
    public Pages pages { get; set; }
}

public class Pages {
    public _4429395 _4429395 { get; set; }
}

public class Normalized{
    public string from { get; set; }
    public string to { get; set; }
}

Thanks for the help

  • Use a dictionary for `pages` as shown in [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/q/34213566/3744182). – dbc Apr 15 '17 at 16:39

2 Answers2

1

Depending on what library you are using for the deserialisation, You could declare Pages property on Query as a Dictionary<string,[contentObject]>. You will need to make a class for the contentObject with the relevant properties (pageId,ns,title etc).

http://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm

App Pack
  • 1,512
  • 10
  • 9
0

You can define your pages property of Query as Dictionary<string, Page> and define a class for Page:

public class Query
{
    public Normalized[] normalized { get; set; }
    public Dictionary<string, Page> pages { get; set; }
}

public class Page
{
    public int pageid { get; set; }
    public int ns { get; set; }
    public string title { get; set; }
    public string extract { get; set; }
}
Saravana
  • 37,852
  • 18
  • 100
  • 108