更新时间:2023-01-17 10:33:51
这里是如何使用 https://github.com/facebook-csharp-sdk/simple-json ( https://nuget.org/packages/SimpleJsona>).
Here is how you do using https://github.com/facebook-csharp-sdk/simple-json ( https://nuget.org/packages/SimpleJson ).
var text = "{"query":{"pages":{"6695":{"pageid":6695,"ns":0,"title":"Citadel","touched":"2012-01-03T19:16:16Z","lastrevid":468683764,"counter":"","length":8899}}}}";
(使用动态)
dynamic json = SimpleJson.DeserializeObject(text);
string title = json.query.pages["6695"].title;
foreach (KeyValuePair<string, dynamic> page in json.query.pages)
{
var id = page.Key;
var pageId = page.Value.pageid;
var ns = page.Value.ns;
}
(使用强类型类)
class result
{
public query query { get; set; }
}
class query
{
public IDictionary<string, page> pages { get; set; }
}
class page
{
public long pageid { get; set; }
public string title { get; set; }
}
var result = SimpleJson.DeserializeObject<result>(text);
[更新]
在不支持动态且您不想使用强类型类的 Windows 手机上.
on windows phone where dynamic is not supported and you don't want to use strongly typed classes.
var json = (IDictionary<string, object>)SimpleJson.DeserializeObject(text);
var query = (IDictionary<string, object>)json["query"];
var pages = (IDictionary<string, object>)query["pages"];
var pageKeys = pages.Keys;
var page = (IDictionary<string, object>)pages["6695"];
var title = (string)page["title"];