一个API正在向我返回以下json:
{
"query":{
"pages":{
"49123":{
"pageid":49123,
"ns":0,
"title":"Phoenix (constellation)",
"revisions":[
{
"revid":588710862,
"parentid":588710834
}
]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我用json2csharp构建了一个代表这个json的类(我手动调整了PageInfo的名字,因为它正在咳嗽这个名字并称之为__invalid_type__49123)
public class Revision
{
public int revid { get; set; }
public int parentid { get; set; }
}
public class PageInfo
{
public int pageid { get; set; }
public int ns { get; set; }
public string title { get; set; }
public List<Revision> revisions { get; set; }
}
public class Pages
{
public PageInfo pageInfo { get; set; }
}
public class Query
{
public Pages pages { get; set; }
}
public class RootObject
{
public Query query { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并试图解析它:
var json = "{\"query\":{\"pages\":{\"49123\":{\"pageid\":49123,\"ns\":0,\"title\":\"Phoenix (constellation)\",\"revisions\":[{\"revid\":588710862,\"parentid\":588710834}]}}}}";
// unescaped version of json below
// {"query":{"pages":{"49123":{"pageid":49123,"ns":0,"title":"Phoenix (constellation)","revisions":[{"revid":588710862,"parentid":588710834}]}}}}
var root = JsonConvert.DeserializeObject<RootObject>(json);
Run Code Online (Sandbox Code Playgroud)
我可以检查并看到root.query.pages都有值,但pageInfo为null.我不知道我错过了什么,这将允许我将这个json加载到一个对象中.
更改您的课程定义如下.它会工作..(见Query课程的定义)
public class Revision
{
public int revid { get; set; }
public int parentid { get; set; }
}
public class Page
{
public int pageid { get; set; }
public int ns { get; set; }
public string title { get; set; }
public List<Revision> revisions { get; set; }
}
public class Query
{
public Dictionary<string,Page> pages { get; set; }
}
public class RootObject
{
public Query query { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |