C# DataContractJsonSerializer 的反序列化问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/596271/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:40:36  来源:igfitidea点击:

Deserialization problem with DataContractJsonSerializer

c#wcfjsondictionaryserialization

提问by bbrown

I've got the following piece of JSON:

我有以下一段 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 labelare required but itemsand rulesare optional.

在上面的示例中namevalue、 和label是必需的,但itemsrules是可选的。

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();

Everything goes reasonably well except that itemsand rulesare empty for the first FooDefpass. I have tried everything under the sun to try and get them populated: custom classes, NameValueCollection, KeyValuePair<string, string>, Listof both of the latter, and every other collection that seemed to apply. [EDIT: I forgot to try Hashtable, which seemed like an obvious candidate. Didn't work.]

一切进展相当不错,除了itemsrules是空的第一个FooDef通行证。我曾尝试一切在阳光下,试图让他们填充:自定义类,NameValueCollectionKeyValuePair<string, string>List无论是后者,和所有其他的集合,似乎适用的。[编辑:我忘了尝试Hashtable,这似乎是一个明显的候选人。没有用。]

The problem, as I see it, is that the key piece under itemsand rulesis open-ended. That is, it's not always going to be called rangeor 3. Any advice or ideas?

这个问题,在我看来,是在关键件itemsrules是开放式的。也就是说,它并不总是被称为rangeor 3。有什么建议或想法吗?

采纳答案by Darin Dimitrov

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

恕我直言,无法使用 DataContractJsonSerializer 将您提供的 JSON 字符串反序列化为 .NET 类。

The problem comes from the way DataContractJsonSerializerserializes 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).

问题来自DataContractJsonSerializer序列化字典的方式。您可以使用替代序列化程序,例如Json.NET(我强烈推荐)或JavaScriptSerializer(我认为它已被弃用,以支持 DataContractJsonSerializer,但它适用于您的场景)。

You can also read theserants.

您也可以阅读这些咆哮

Documentation: Serializing Collections - Json.NET

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

回答by Sean Kelly

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/071f73bb-e141-4a68-ae61-05635382934f

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/071f73bb-e141-4a68-ae61-05635382934f

Check this article out - it solved my problem almost perfectly. I had to change their object[] Type to a string, but i'm only using strings:string type Key/Value pairs, so no problems there.

看看这篇文章 - 它几乎完美地解决了我的问题。我不得不将他们的 object[] Type 更改为字符串,但我只使用 strings:string type Key/Value 对,所以没有问题。