从json字符串中获取数据

时间:2018-01-29 12:30:02

标签: c# json json.net

我想从

中保存“title”值
https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&titles=usa&redirects=true

我使用此代码但不起作用:

string fileDownload;
using (WebClient client = new WebClient())
{
    fileDownload = client.DownloadString("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&titles=usa&redirects=true");
}
JObject sa = JObject.Parse(fileDownload);

string sq = sa["query"]["pages"][0]["title"].ToString();

我在[0]之后使用["pages"],因为其他网页的pageid更改。

1 个答案:

答案 0 :(得分:2)

尝试以下

string sq = sa["query"]["pages"].First.First["title"].ToString();