C#中的JSON解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON decoding in c#
提问by raki
how to decode a json response in c#?
如何在 C# 中解码 json 响应?
采纳答案by raki
Check out the DataContractJsonSerializer. You'll have to target .NET 3.5, which means Visual Studio 2008 is pretty much required. Here's a good blog postabout using the Json data contract serializer.
查看DataContractJsonSerializer。您必须以 .NET 3.5 为目标,这意味着几乎需要 Visual Studio 2008。 这是一篇关于使用 Json 数据协定序列化程序的好博文。
回答by Preet Sangha
回答by Powerlord
In addition to the 3.5 methods above, if you install the ASP.NET 2.0 AJAX Extensions 1.0(2.0 is the framework version), you will gain the System.Web.Script.Serialization.JavaScriptSerializerclass, which can encode/decode json.
除了上面的3.5方法,如果你安装ASP.NET 2.0 AJAX Extensions 1.0(2.0是框架版本),你会得到System.Web.Script.Serialization.JavaScriptSerializer类,可以对json进行编码/解码。
回答by Stefan Steiger
The .NET integrated classes have their merits. But they have their shortcomings.
.NET 集成类有其优点。但是他们有他们的缺点。
For example, DataContractJsonSerializer is not available in .NET 2.0, System.Web.Extensions needs admin rights to install it (in NET 2.0 - you can localcopy it, if you don't have a WebSite project) plus it doesn't work in SilverLight and WindowsPhone. If you have a WebSite project, you need to copy the System.Web.Extensions assemblies to your project, and remove them from GAC afterwards, else VisualStudio doesn't understand it has to localcopy them.
例如,DataContractJsonSerializer 在 .NET 2.0 中不可用,System.Web.Extensions 需要管理员权限才能安装它(在 NET 2.0 中 - 如果你没有网站项目,你可以本地复制它)而且它在SilverLight 和 WindowsPhone。如果你有一个网站项目,你需要将 System.Web.Extensions 程序集复制到你的项目中,然后从 GAC 中删除它们,否则 VisualStudio 不明白它必须本地复制它们。
But more importantly, if you work with pretty much any JavaScript library, e.g. SlickGrid (AJAX grid), you will stumble upon this valid JavaScript object (but it's invalid JSON, because fnFormatDate_DE is a function call and not text, it lacks the quotation marks):
但更重要的是,如果您使用几乎任何 JavaScript 库,例如 SlickGrid(AJAX 网格),您会偶然发现这个有效的 JavaScript 对象(但它是无效的 JSON,因为 fnFormatDate_DE 是一个函数调用而不是文本,它缺少引号):
FormatterCallback :
{
name : "DateFormatter_DE"
func: fnFormatDate_DE(val)
}
No chance to serialize this with any of the .NET integrated classes (because it's invalid JSON). Also, they fall short in terms of performance, availability in SilverLight, Windows Phone and WindowsRT. They are neither OpenSource nor MIT license. They have no support for indentation (human readable JSON), and they can't serialize DataTables, and they have problems with circular references. You can't handle serialization errors with them, can't serialize enums to their names, and you can't switch the date format (OK, this is not really a problem, because the MS date format is the only date format the safari crap understands [it doesn't undestand ISO]), and they don't serialize neither nHibernate nor Entity...
没有机会用任何 .NET 集成类序列化它(因为它是无效的 JSON)。此外,它们在 SilverLight、Windows Phone 和 WindowsRT 的性能、可用性方面也存在不足。它们既不是开源也不是 MIT 许可证。它们不支持缩进(人类可读的 JSON),并且它们不能序列化 DataTables,并且它们在循环引用方面存在问题。您无法使用它们处理序列化错误,无法将枚举序列化为其名称,也无法切换日期格式(好吧,这不是真正的问题,因为 MS 日期格式是 safari 中唯一的日期格式废话理解[它不理解 ISO]),并且它们既不序列化 nHibernate 也不序列化实体......
But most importantly, you won't want to switch your library or adjust project references if you go from .NET 2.0 to 4.0, you don't want to rewrite your code if you want to use some code in SilverLight/Windows Phone, and you don't want to be write a function to beautify JSON if you want to look whether you got the class right, and you won't want to write your own method to strip out quotation marks just because Microsoft's libraries can't handle invalid JSON.
但最重要的是,如果您从 .NET 2.0 到 4.0,您将不想切换您的库或调整项目引用,如果您想在 SilverLight/Windows Phone 中使用某些代码,您不想重写您的代码,并且如果您想查看类是否正确,您不想编写一个函数来美化 JSON,并且您不想编写自己的方法来去掉引号,因为 Microsoft 的库无法处理无效的JSON。
Also, Microsoft's libraries have a low performance, and they can't serialize to BSON (for use with NoSQL databases like MongoDB).
So for all these reasons, you better choose NewtonSoft JSON (JSON.NET).
It's free and OpenSource (MIT license, not GPL).
There is a nice comparison matrix here:
http://james.newtonking.com/pages/json-net.aspx
此外,Microsoft 的库性能较低,并且无法序列化为 BSON(用于与 MongoDB 等 NoSQL 数据库一起使用)。
因此,出于所有这些原因,您最好选择 NewtonSoft JSON (JSON.NET)。
它是免费的和开源的(MIT 许可证,不是GPL)。
这里有一个很好的比较矩阵:http:
//james.newtonking.com/pages/json-net.aspx