Extract data from Json string(从 Json 字符串中提取数据)
问题描述
我得到了一个包含 Json 的字符串.它看起来像这样:
I got a string containing Json. It looks like this:
"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,我在这里发现了类似的问题..
I am trying to get a hold of the "img_url". I have Json.NET installed and I´ve found similar questions here..
例如这样的:
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..没有运气
In my case I changed ("People[0].Name") to ("img_url"),("img_url[0]) etc..no luck
这是我现在的代码:
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;
}
}
在提取值之前,我是否必须对字符串进行处理?谢谢!
Do I have to do something with the string before I can extract the value? Thanks!
推荐答案
img_url 不是根对象的属性 - 它是 data 对象的属性:
img_url is not a property of root object - it's a property of data object:
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 字符串中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Json 字符串中提取数据
基础教程推荐
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
