Hamed.Bny
Hamed.Bny

Reputation: 95

Get data from a json string

I want to save "title" value from

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&titles=usa&redirects=true

I use this code but doesn't work:

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();

I use [0] after ["pages"] because pageid changes for another page.

Upvotes: 2

Views: 85

Answers (1)

tchelidze
tchelidze

Reputation: 8308

Try following

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

Upvotes: 2

Related Questions