C# System.Web.Script.Serialization.JavaScriptSerializer 还是 System.Runtime.Serialization.Json.DataContractJsonSerializer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/831697/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:24:31  来源:igfitidea点击:

System.Web.Script.Serialization.JavaScriptSerializer or System.Runtime.Serialization.Json.DataContractJsonSerializer?

c#.netjson

提问by Shawn Miller

What's the difference between the two? Why would you use one over the other?

两者有什么区别?你为什么要使用一个?

采纳答案by Kamarey

Found here: http://aaron-powell.spaces.live.com/blog/cns!91A824220E2BF369!150.entry

在这里找到:http: //aaron-powell.spaces.live.com/blog/cns!91A824220E2BF369!150.entry

DataContractJsonSerializerThe primary purpose of the DataContractJsonSerializer is to be used with WCF, since one serialization is a big focus of WCF. Also, it is also better equipped to handle complex classes which have only certain properties available for serialization. This class is more strongly typed, has more knowledge about the type(s) it's handling and better error handling for badly-formed JSON.

DataContractJsonSerializer DataContractJsonSerializer的主要目的是与 WCF 一起使用,因为序列化是 WCF 的一大重点。此外,它还可以更好地处理只有某些属性可用于序列化的复杂类。这个类是更强类型的,对它处理的类型有更多的了解,并且对格式错误的 JSON 有更好的错误处理。

JavaScriptSerializerThis class on the other hand is much better equipped for quick serialization, it's a more cowboy approach. There's less error checking and less control over what properties which are serialized.

JavaScriptSerializer另一方面,这个类更适合快速序列化,它是一种更牛仔的方法。错误检查较少,对序列化的属性的控制也较少。

Update

更新

As the above link is dead, here is another link: http://kb.cnblogs.com/a/1454030.

由于上面的链接已经失效,这里还有一个链接:http: //kb.cnblogs.com/a/1454030

回答by W. Kevin Hazzard

The JavaScriptSerializer is marked as obsolete in framework 3.5. You shouldn't use it for that reason. However, back to your question. The JavaScriptSerializer doesn't require classes to be marked as [Serializable] or as [DataContract] but the DataContractJsonSerializer does. If you have compiled classes that you can't mark with attributes, you may want to use the older JSON serializer.

JavaScriptSerializer 在框架 3.5 中被标记为过时。你不应该因为这个原因使用它。然而,回到你的问题。JavaScriptSerializer 不需要将类标记为 [Serializable] 或 [DataContract],但 DataContractJsonSerializer 需要。如果您编译了无法用属性标记的类,您可能需要使用旧的 JSON 序列化程序。

回答by Marc Gravell

Personally, I'd look at Json.NET- this has the advantage of being .NET 2.0 compatible

就个人而言,我会看看Json.NET- 这具有与 .NET 2.0 兼容的优势