本文介绍了我如何解析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?
推荐答案
我不使用假设。如果这样的话,那么你应该尝试一下。
I am assuming you are not using Json.NET. If this the case, then you should try it.
它具有以下特点:
- 的LINQ to JSON
- 的JsonSerializer快速转换您的.NET对象,JSON,然后再返回
- Json.NET可以选择性地产生良好的格式化,调试或显示 缩进JSON
- 属性像JsonIgnore和JsonProperty可以被添加到类来定制类是如何序列
- 能够转换JSON和XML
- 支持多种平台:.NET,Silverlight和Compact Framework的
- LINQ to JSON
- The JsonSerializer for quickly converting your .NET objects to JSON and back again
- Json.NET can optionally produce well formatted, indented JSON for debugging or display
- Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized
- Ability to convert JSON to and from XML
- Supports multiple platforms: .NET, Silverlight and the Compact Framework
看那的。在这个例子中,<$c$c>JsonConvert$c$c>对象是用来将对象转换为和从JSON。它为此两个静态方法。它们是<$c$c>SerializeObject(Object OBJ) 和<$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#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!