简体   繁体   English

在C#中反序列化不断变化的JSON对象名称

[英]Deserialize ever changing JSON object name in C#

The endpoint I'm requesting from: 我从以下位置请求的端点:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=eminem https://zh.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. 如果您查看Pages对象内部的JSON,则还有另一个对象,它是Wikipedia文章的ID。 And in my project, I won't be only parsing Eminem's page but other singers/artists. 在我的项目中,我不仅会解析Eminem的页面,还会解析其他歌手/艺术家。 So my question is how can I go and parse this ever changing json object name? 所以我的问题是我该如何解析这个不断变化的json对象名称?

My json as c# classes 我的JSON作为C#类

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 谢谢您的帮助

Depending on what library you are using for the deserialisation, You could declare Pages property on Query as a Dictionary<string,[contentObject]> . 根据要用于反序列化的库,可以在Query上将其Pages属性声明为Dictionary<string,[contentObject]> You will need to make a class for the contentObject with the relevant properties (pageId,ns,title etc). 您将需要使用相关属性(pageId,ns,title等)为contentObject创建一个类。

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

You can define your pages property of Query as Dictionary<string, Page> and define a class for Page : 您可以将Querypages属性定义为Dictionary<string, Page>并为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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM