C#中的Json序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13278459/
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 Serialization in C#
提问by Brandon Clapp
I'm trying to serialize a local object to json but msdn documentation always seems to confuse me. I believe I am suppose to use the DataContractJsonSerializer but not completely sure, as I have seen mixed responses. I've also had someone recommend Newtonsoft.
我正在尝试将本地对象序列化为 json,但 msdn 文档似乎总是让我感到困惑。我相信我应该使用 DataContractJsonSerializer 但不完全确定,因为我看到了不同的反应。我也有人推荐了 Newtonsoft。
Does anyone have any experience with this that can point me in the right direction?
有没有人有这方面的经验可以为我指明正确的方向?
采纳答案by Chris Farmer
You could use the JavaScriptSerializer.
您可以使用 JavaScriptSerializer。
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
var thing = new Thing();
var json = new JavaScriptSerializer().Serialize(thing);
回答by lstern
Unless you have some very specific json needs, stick with the framework serializer. You will find more feedback on eventual problems.
除非您有一些非常具体的 json 需求,否则请坚持使用框架序列化程序。您会发现有关最终问题的更多反馈。
回答by Marcus
There are so many different json libraries in .NET, which provide for some sort of serialization. Look at the bottom of http://json.org/to find a list of JSON libraries for C#. Whichever one is best depends on your needs. I've used json.net from Newtonsoft, although personally I have found the serializer to be very slow. If you need speed Servicestack claims to have the fastest one (here). The site also shows some performance comparisons, although I have not tried it.
.NET 中有许多不同的 json 库,它们提供某种序列化。查看http://json.org/的底部以查找 C# 的 JSON 库列表。哪个最好取决于您的需求。我使用了 Newtonsoft 的 json.net,但我个人发现序列化程序非常慢。如果您需要速度 Servicestack 声称拥有最快的速度(这里)。该站点还显示了一些性能比较,尽管我还没有尝试过。
Do some Googling, or simply try them out, write some tests, or just pick the framework one and introduce no extra dependencies and be done with it.
做一些谷歌搜索,或者简单地尝试一下,编写一些测试,或者只是选择一个框架并且不引入额外的依赖项并完成它。
回答by mythz
Well ServiceStack's JSON serializer isthe fastest .NET JSON serializer.
ServiceStack 的 JSON 序列化器是最快的.NET JSON 序列化器。
And weighing at only 159kbit's also very lightweight, not too mention contains lots of useful features, e.g. .NET's fastest JSVand CSVtext serializers, T.Dump()utils + more.
并且仅重159kb它也非常轻巧,更不用说包含许多有用的功能,例如 .NET 最快的JSV和CSV文本序列化程序,T.Dump()utils + 更多。

