博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用C#解析JSON?
阅读量:2380 次
发布时间:2019-05-10

本文共 4050 字,大约阅读时间需要 13 分钟。

本文翻译自:

I have the following code: 我有以下代码:

var user = (Dictionary
)serializer.DeserializeObject(responsecontent);

The input in responsecontent is JSON, but it is not properly parsed into an object. responsecontent的输入为JSON,但未正确解析为对象。 How should I properly deserialize it? 如何正确反序列化?


#1楼

参考:


#2楼

As was answered here - 正如这里回答的那样-将

It's pretty simple using Json.NET: 使用Json.NET非常简单:

dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");string name = stuff.Name;string address = stuff.Address.City;

Or using Newtonsoft.Json.Linq : 或使用Newtonsoft.Json.Linq:

dynamic stuff = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");string name = stuff.Name;string address = stuff.Address.City;

#3楼

Here are some options without using third party libraries: 以下是一些使用第三方库的选项:

// For that you will need to add reference to System.Runtime.Serializationvar jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(@"{ ""Name"": ""Jon Smith"", ""Address"": { ""City"": ""New York"", ""State"": ""NY"" }, ""Age"": 42 }"), new System.Xml.XmlDictionaryReaderQuotas());// For that you will need to add reference to System.Xml and System.Xml.Linqvar root = XElement.Load(jsonReader);Console.WriteLine(root.XPathSelectElement("//Name").Value);Console.WriteLine(root.XPathSelectElement("//Address/State").Value);// For that you will need to add reference to System.Web.Helpersdynamic json = System.Web.Helpers.Json.Decode(@"{ ""Name"": ""Jon Smith"", ""Address"": { ""City"": ""New York"", ""State"": ""NY"" }, ""Age"": 42 }");Console.WriteLine(json.Name);Console.WriteLine(json.Address.State);

See the link for more information about . 请参阅链接以获取有关更多信息。

Update : Nowadays the easiest way to get the Web.Helpers is to use the . 更新 :如今,获取Web.Helpers的最简单方法是使用 。


If you don't care about earlier windows versions you can use the classes of the namespace: 如果您不关心早期的Windows版本,则可以使用命名空间的类:

// minimum supported version: Win 8JsonObject root = Windows.Data.Json.JsonValue.Parse(jsonString).GetObject();Console.WriteLine(root["Name"].GetString());Console.WriteLine(root["Address"].GetObject()["State"].GetString());

#4楼

Another native solution to this, which doesn't require any 3rd party libraries but a reference to System.Web.Extensions is the JavaScriptSerializer. 另一个本机解决方案,不需要任何第三方库,但对System.Web.Extensions的引用是JavaScriptSerializer。 This is not a new but a very unknown built-in features there since 3.5. 自3.5以来,这不是一个新功能,而是一个非常未知的内置功能。

using System.Web.Script.Serialization;

.. ..

JavaScriptSerializer serializer = new JavaScriptSerializer();objectString = serializer.Serialize(new MyObject());

and back 然后回来

MyObject o = serializer.Deserialize
(objectString)

#5楼

The following from the site should I think help provide some native functionality for what you are looking for. 我认为站点上的以下内容应为您所寻找的内容提供一些本机功能。 Please note it is specified for Windows 8. One such example from the site is listed below. 请注意,它是为Windows 8指定的。下面列出了该站点的一个示例。

JsonValue jsonValue = JsonValue.Parse("{\"Width\": 800, \"Height\": 600, \"Title\": \"View from 15th Floor\", \"IDs\": [116, 943, 234, 38793]}");double width = jsonValue.GetObject().GetNamedNumber("Width");double height = jsonValue.GetObject().GetNamedNumber("Height");string title = jsonValue.GetObject().GetNamedString("Title");JsonArray ids = jsonValue.GetObject().GetNamedArray("IDs");

It utilizes the namespace. 它利用命名空间。


#6楼

Try the following code: 尝试以下代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL");JArray array = new JArray();using (var twitpicResponse = (HttpWebResponse)request.GetResponse())using (var reader = new StreamReader(twitpicResponse.GetResponseStream())){    JavaScriptSerializer js = new JavaScriptSerializer();    var objText = reader.ReadToEnd();    JObject joResponse = JObject.Parse(objText);    JObject result = (JObject)joResponse["result"];    array = (JArray)result["Detail"];    string statu = array[0]["dlrStat"].ToString();}

转载地址:http://ofexb.baihongyu.com/

你可能感兴趣的文章
去除firefox点击链接时的虚线边框
查看>>
php中截取中文字符串
查看>>
magento 通过属性获取产品
查看>>
php数组和对象的值传递和引用传递
查看>>
div 垂直居中对齐方法收集
查看>>
php curl请求信息和返回信息
查看>>
JavaScript importScripts 之实现和用法
查看>>
html5 web worker 和serverSend使用
查看>>
html5 drawImage 不显示问题
查看>>
node.js web应用基本框架
查看>>
node.js站点备份
查看>>
node.js文件上传处理
查看>>
nodejs常用工具util
查看>>
js 模版引擎jade使用语法
查看>>
node.js express 运行环境 NODE_ENV
查看>>
magento修改页面标题的3种方法
查看>>
图解HTTPS
查看>>
html5鼠标滚轮事件mousewheel使用
查看>>
js遍历对象属性和值
查看>>
js 区分鼠标左右键
查看>>