什么办法能让DataContractJsonSerializer正确序列化字典? [英] Any way to make DataContractJsonSerializer serialize Dictionaries properly?
问题描述
该DataContractJsonSerializer不能正确序列化词典。
而JavaScriptSerializer连载词典作为 {ABC:某某,高清:42}
例如,
在DataContractJsonSerializer给 [{关键:ABC,值:XYZ},{关键:高清,值:42}]
代替。
这的确是有问题,我想知道我怎么能在我的WCF服务正确序列化Dictionary对象。我找的,将至少需要努力量的解决方案。
REF:<一href="http://msdn.microsoft.com/en-us/library/bb412170.aspx">http://msdn.microsoft.com/en-us/library/bb412170.aspx
这是我最后用在WCF正确serilize词典解决办法:<一href="http://social.msdn.microsoft.com/forums/en-US/wcf/thread/765f1569-0422-4471-8ec2-1d03b2026771">http://social.msdn.microsoft.com/forums/en-US/wcf/thread/765f1569-0422-4471-8ec2-1d03b2026771
无功序列化=新DataContractJsonSerializer(typeOfObj,新DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat =真
});
The DataContractJsonSerializer is not able to serialize Dictionaries properly.
Whereas JavaScriptSerializer serializes Dictionaries as {"abc":"xyz","def":42}
for example,
the DataContractJsonSerializer gives [{"Key":"abc","Value":"xyz"},{"Key":"def","Value":42}]
instead.
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.
ref: 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
var serializer = new DataContractJsonSerializer(typeOfObj, new DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat = true
});
这篇关于什么办法能让DataContractJsonSerializer正确序列化字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!