DataContractJsonSerializer的反序列化问题 [英] Deserialization problem with DataContractJsonSerializer

查看:104
本文介绍了DataContractJsonSerializer的反序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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:\"}]

在上面的例子中,名称标签是必需的,但项目规则是可选的。

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; }
    }
}

这是我用来反序列化的代码: p>

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 pass是空的。我已经尝试过一切在太阳下尝试并让它们填充:自定义类, NameValueCollection KeyValuePair< string,string> 列表,后者和每个其他收藏似乎都适用。

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.

我看到的问题是,项目规则是开放式的。也就是说,并不总是被称为 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?

推荐答案

IMHO没有办法使用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).

您还可以阅读这些 rants

文档: 序列化集合 - Json.NET

Documentation: Serializing Collections - Json.NET

这篇关于DataContractJsonSerializer的反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆