.net 有什么方法可以让 DataContractJsonSerializer 正确序列化字典?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4559991/
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
Any way to make DataContractJsonSerializer serialize Dictionaries properly?
提问by morpheus
The DataContractJsonSerializer is not able to serialize Dictionaries properly.
DataContractJsonSerializer 无法正确序列化字典。
Whereas JavaScriptSerializer serializes Dictionaries as {"abc":"xyz","def":42}for example,
the DataContractJsonSerializer gives [{"Key":"abc","Value":"xyz"},{"Key":"def","Value":42}]instead.
JavaScriptSerializer 序列化字典{"abc":"xyz","def":42},例如,DataContractJsonSerializer 提供[{"Key":"abc","Value":"xyz"},{"Key":"def","Value":42}]。
This is really problematic and I want to know how can I serialize Dictionary objects correctly in my WCF service. I am looking for a solution that would require least amount of effort.
这确实有问题,我想知道如何在 WCF 服务中正确序列化 Dictionary 对象。我正在寻找一种需要最少努力的解决方案。
ref: http://msdn.microsoft.com/en-us/library/bb412170.aspx
参考:http: //msdn.microsoft.com/en-us/library/bb412170.aspx
This is the workaround I finally used to serilize dictionaries properly in WCF: http://social.msdn.microsoft.com/forums/en-US/wcf/thread/765f1569-0422-4471-8ec2-1d03b2026771
这是我最终用来在 WCF 中正确序列化字典的解决方法:http://social.msdn.microsoft.com/forums/en-US/wcf/thread/765f1569-0422-4471-8ec2-1d03b2026771
回答by Mr.Wang from Next Door
Using DataContractJsonSerializerSettings(available since .NET 4.5) can do this for you:
使用DataContractJsonSerializerSettings(从 .NET 4.5 开始可用)可以为您做到这一点:
var serializer = new DataContractJsonSerializer(typeOfObj, new DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat = true
});
回答by Josh
Unfortunately this appears to be by-design, according to the section "Collections, Dictionaries, and Arrays" at http://msdn.microsoft.com/en-us/library/bb412170.aspx
不幸的是,根据http://msdn.microsoft.com/en-us/library/bb412170.aspx 上的“集合、字典和数组”部分,这似乎是设计使然
All collections, dictionaries, and arrays are represented in JSON as arrays.
所有集合、字典和数组都在 JSON 中表示为数组。
回答by faester
Although this will in most cases cause a major rewrite and thus not be feasible you can let your WCF service interface accept and return Streamin which case you can take full control of serialization. This way you can use JavaScriptSerializer, JSON.NETor ServiceStack.JSONto perform the actual serialization and these serializers actually deals with a dictionaries in a more sensible way.
尽管这在大多数情况下会导致重大重写,因此不可行,但您可以让 WCF 服务接口接受并返回,Stream在这种情况下,您可以完全控制序列化。通过这种方式,您可以使用JavaScriptSerializer、JSON.NET或ServiceStack.JSON来执行实际的序列化,而这些序列化器实际上以更明智的方式处理字典。
回答by Colin
DataContractJsonSerializerSettingshas the UseSimpleDictionaryFormatproperty now and it serializes dictionaries the way you'd expect.
DataContractJsonSerializerSettingsUseSimpleDictionaryFormat现在拥有该属性,它以您期望的方式序列化字典。

