Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Json字符串中提取数据_C#_Json_Json.net - Fatal编程技术网

C# 从Json字符串中提取数据

C# 从Json字符串中提取数据,c#,json,json.net,C#,Json,Json.net,我得到了一个包含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", "im

我得到了一个包含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"
}
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");
我正在尝试获取“img_url”。我已经安装了Json.NET,我在这里发现了类似的问题

例如,类似这样的事情:

"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"
}
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");
在我的例子中,我把
(“人[0].Name”)
改为
(“img\u url”)
(“img\u url[0])
等等。运气不好

这是我现在的代码:

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;
    }
}
在提取值之前,是否必须对字符串执行某些操作? 谢谢!

您还可以使用
dynamic
关键字执行相同的操作(不声明上述类)


img\u 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。“img_url”属性是JSON对象的一部分,它又被分配给一个JSON属性。该属性的名称是什么?您的意思是数据?Im丢失?是的,“数据”。因此您应该使用
SelectToken(“data.img_url”)
现在感觉很傻..尝试了很多组合,但不是这个..谢谢!我正在编写答案。Mark Sergey的答案很好。但是请注意一些区别:如果“data”对象不在JSON中,Sergey的代码将抛出异常,而SelectToken将只返回null(不确定这是否与您的应用程序场景相关)@elgonzo谢谢。使用索引器读取属性并不涉及解析JPath,但我喜欢SelectToken选项,因为它具有更好的可读性(而且更安全)
var url = (string)obj.SelectToken("data.img_url");