我如何解析JSON用C#? [英] How can I parse JSON with C#?

查看:90
本文介绍了我如何解析JSON用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code

Dictionary<string, object> user = (Dictionary<string, object>)serializer.DeserializeObject(responsecontent);

responsecontent 输入是JSON,但它不能正确解析成JSON对象。我应该如何正确序列化呢?

The input in responsecontent is JSON, but it is not properly parsed into a JSON object. How should I properly serialize it?

推荐答案

我不使用 Json.NET 假设。如果这样的话,那么你应该尝试一下。

I am assuming you are not using Json.NET. If this the case, then you should try it.

它具有以下特点:


  1. 的LINQ to JSON

  2. 的JsonSerializer快速转换您的.NET对象,JSON,然后再返回

  3. Json.NET可以选择性地产生良好的格式化,调试或显示
  4. 缩进JSON
  5. 属性像JsonIgnore和JsonProperty可以被添加到类来定制类是如何序列

  6. 能够转换JSON和XML

  7. 支持多种平台:.NET,Silverlight和Compact Framework的

  1. LINQ to JSON
  2. The JsonSerializer for quickly converting your .NET objects to JSON and back again
  3. Json.NET can optionally produce well formatted, indented JSON for debugging or display
  4. Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized
  5. Ability to convert JSON to and from XML
  6. Supports multiple platforms: .NET, Silverlight and the Compact Framework

看那下面的例子的。在这个例子中,<一href=\"http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm\"><$c$c>JsonConvert对象是用来将对象转换为和从JSON。它为此两个静态方法。它们是<一个href=\"http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonConvert_SerializeObject.htm\"><$c$c>SerializeObject(Object OBJ) 和<一个href=\"http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonConvert_DeserializeObject__1.htm\"><$c$c>DeserializeObject<T>(String JSON) :

Look at the example below. In this example, JsonConvert object is used to convert an object to and from JSON. It has two static methods for this purpose. They are SerializeObject(Object obj) and DeserializeObject<T>(String json):

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);

这篇关于我如何解析JSON用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆