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

查看:290
本文介绍了反序列化问题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的行版本(适用于字符串):

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

这里的code我用反序列化:

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 通过。我已经在阳光下想尽一切办法试图让他们填充:自定义类,的NameValueCollection KeyValuePair&LT;字符串,字符串&GT; 列表这两个后者的,和所有其他的集合,似乎适用。

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.

问题,在我看来,是项目规则下的关键部分是开放式的。也就是说,它并不总是会被称为范围 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提供到.NET类的JSON字符串。

IMHO there is no way to deserialize the JSON string you provided into a .NET class using DataContractJsonSerializer.

问题来自于路 DataContractJsonSerializer序列化的字典。你可以使用另一种串行如 Json.NET (我强烈推荐)或<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx\"相对=nofollow>的JavaScriptSerializer (我认为这是有利于DataContractJsonSerializer的pcated德$ P $,但它会为您的方案工作)。

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).

您还可以阅读这些 咆哮

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

Documentation: Serializing Collections - Json.NET

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

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