本文共 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? 如何正确反序列化?
参考:
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;
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());
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)
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. 它利用命名空间。
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/