从Json字符串中提取数据

我有一个包含Json的字符串。看起来像这样:

"status_code":200,

"status_txt":"OK",

"data":

{

"img_name":"D9Y3z.png",

"img_url":"http:\/\/s1.uploads.im\/D9Y3z.png",

"img_view":"http:\/\/uploads.im\/D9Y3z.png",

"img_width":"167",

"img_height":"288",

"img_attr":"width=\"167\" height=\"288\"",

"img_size":"36.1 KB",

"img_bytes":36981,

"thumb_url":"http:\/\/s1.uploads.im\/t\/D9Y3z.png",

"thumb_width":360,

"thumb_height":360,

"source":"http:\/\/www.google.com\/images\/srpr\/nav_logo66.png",

"resized":"0",

"delete_key":"df149b075ab68c38"

}

我正在尝试保留“ img_url”。我已经安装了Json.NET,并且在这里发现了类似的问题。

例如这样的事情:

JObject o = JObject.Parse("{'People':[{'Name':'Jeff'},{'Name':'Joe'}]}");

// get name token of first person and convert to a string

string name = (string)o.SelectToken("People[0].Name");

在我来说,我改变("People[0].Name")("img_url")("img_url[0])etc..no运气

现在这是我的代码:

public string tempJson { get; set; }

public ActionResult SaveUploadedFile(string test)

{

using (WebResponse wrs = wrq.GetResponse())

using (Stream stream = wrs.GetResponseStream())

using (StreamReader reader = new StreamReader(stream))

{

string json = reader.ReadToEnd();

tempJson = json;

}

}

在提取值之前,是否必须对字符串做一些事情?谢谢!

回答:

img_url不是根对象的属性-它是data对象的属性:

var obj = JObject.Parse(json);

var url = (string)obj["data"]["img_url"]; // http://s1.uploads.im/D9Y3z.png

另外的选择:

var url = (string)obj.SelectToken("data.img_url");

以上是 从Json字符串中提取数据 的全部内容, 来源链接: utcz.com/qa/430480.html

回到顶部