DataContractJsonSerializer 的反序列化问题 [英] Deserialization problem with DataContractJsonSerializer
问题描述
我有以下 JSON 片段:
I've got the following piece of JSON:
[{
"name": "numToRetrieve",
"value": "3",
"label": "Number of items to retrieve:",
"items": {
"1": "1",
"3": "3",
"5": "5"
},
"rules": {
"range": "1-2"
}
},
{
"name": "showFoo",
"value": "on",
"label": "Show foo?"
},
{
"name": "title",
"value": "Foo",
"label": "Foo:"
}]
单行版本(适用于字符串文字):
All in one line version (suitable for a string literal):
[{"name":"numToRetrieve","value":"3","label":"Number of items to retrieve:","items":{"1":"1","3":"3","5":"5"},"rules":{"range":"1-2"}},{"name":"showFoo","value":"on","label":"Show foo?"},{"name":"title","value":"Foo","label":"Foo:"}]
在上面的例子中,name
、value
和 label
是必需的,但 items
和 规则
是可选的.
In the above example, name
, value
, and label
are required but items
and rules
are optional.
这是我要反序列化的类:
Here's the class I'm trying to deserialize into:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace foofoo
{
[DataContract]
public sealed class FooDef
{
[DataMember(Name = "name", IsRequired = true)]
public string Name { get; set; }
[DataMember(Name = "value", IsRequired = true)]
public string Value { get; set; }
[DataMember(Name = "label", IsRequired = true)]
public string Label { get; set; }
[DataMember(Name = "items", IsRequired = false)]
public Dictionary<string, string> Items { get; set; }
[DataMember(Name = "rules", IsRequired = false)]
public Dictionary<string, string> Rules { get; set; }
}
}
这是我用来反序列化的代码:
Here's the code I use to deserialize:
var json = new DataContractJsonSerializer(typeof(List<FooDef>));
var bar = "[{"name":"numToRetrieve","value":"3","label":"Number of items to retrieve:","items":{"1":"1","3":"3","5":"5"},"rules":{"range":"1-2"}},{"name":"showFoo","value":"on","label":"Show foo?"},{"name":"title","value":"Foo","label":"Foo:"}]";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(bar));
var foo = json.ReadObject(stream);
stream.Close();
除了第一遍 FooDef
的 items
和 rules
为空外,一切都进行得相当顺利.我已经尝试了所有方法来尝试填充它们:自定义类、NameValueCollection
、KeyValuePair
、List
后者,以及似乎适用的所有其他集合.
Everything goes reasonably well except that items
and rules
are empty for the first FooDef
pass. I have tried everything under the sun to try and get them populated: custom classes, NameValueCollection
, KeyValuePair<string, string>
, List
of both of the latter, and every other collection that seemed to apply.
在我看来,问题在于items
和rules
下的关键部分是开放式的.也就是说,它并不总是被称为 range
或 3
.有什么建议或想法吗?
The problem, as I see it, is that the key piece under items
and rules
is open-ended. That is, it's not always going to be called range
or 3
. Any advice or ideas?
推荐答案
恕我直言,无法使用 DataContractJsonSerializer 将您提供的 JSON 字符串反序列化为 .NET 类.
IMHO there is no way to deserialize the JSON string you provided into a .NET class using DataContractJsonSerializer.
问题来自DataContractJsonSerializer 序列化字典的方式.您可以使用替代序列化程序,例如 Json.NET(我强烈推荐)或 JavaScriptSerializer(我认为是不推荐使用 DataContractJsonSerializer 但它适用于您的场景).
The problem comes from the way DataContractJsonSerializer serializes dictionaries. You could use an alternative serializer such as Json.NET (which I strongly recommend) or JavaScriptSerializer (I think it was deprecated in favor of DataContractJsonSerializer but it will work for your scenario).
Documentation: Serializing Collections - Json.NET
这篇关于DataContractJsonSerializer 的反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!