简体   繁体   English

JSON 使用 C# 中的嵌套列表反序列化

[英]JSON deserialize with nested list in C#

I am serializing and deserializing a Dictionary in C#.我正在序列化和反序列化 C# 中的字典。 My data structure is:我的数据结构是:

    public class DataField
{
    private string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
        }
    }

    private string _Label;
    public string Label
    {
        get { return _Label; }
        set
        {
            _Label = value;
        }
    }

    private ControlType _TypeOfControl;
    public ControlType TypeOfControl
    {
        get { return _TypeOfControl; }
        set { _TypeOfControl = value; }
    }

    private int _Width;
    public int Width
    {
        get { return _Width; }
        set
        {
            _Width = value;
        }
    }
    private DataType _DataType;
    public DataType DataType
    {
        get { return _DataType; }
        set { _DataType = value; }
    }

    private Control _ControlAccessor;
    public Control ControlAccessor
    {
        get { return _ControlAccessor; }
        set { _ControlAccessor = value; }
    }


    private Type _ControlType;
    public Type ControlType
    {
        get { return _ControlType; }
        set { _ControlType = value; }
    }

    private List<KeyValuePair<string, string>> _Items;
    public List<KeyValuePair<string, string>> Items
    {
        get { if (_Items == null) _Items = new List<KeyValuePair<string, string>>(); return _Items; }
        set { _Items = value; }
    }

The object I am serializing is Dictionary<string, DataField>() .我正在序列化的 object 是Dictionary<string, DataField>() I am doing:我在做:

JavaScriptSerializer js = new JavaScriptSerializer();
string str = js.Serialize(ht);`

The string I get is correct:我得到的字符串是正确的:

{"Title" {"Name":"Title","Label":null,"TypeOfControl":0,"Width":200,"DataType":0,"ControlAccessor":null,"ControlType":null,"Items":[]}
,"Price":{"Name":"Price","Label":null,"TypeOfControl":0,"Width":100,"DataType":1,"ControlAccessor":null,"ControlType":null,"Items":[]}
,"Category":{"Name":"Category","Label":null,"TypeOfControl":0,"Width":100,"DataType":1,"ControlAccessor":null,"ControlType":null,"Items":[]}
,"Test":{"Name":"Test","Label":null,"TypeOfControl":0,"Width":100,"DataType":1,"ControlAccessor":null,"ControlType":null,"Items":[]}
,radioGeo":{"Name":"radioGeo","Label":null,"TypeOfControl":3,"Width":0,"DataType":0,"ControlAccessor":null,"ControlType":null
,"Items":[{"Key":"Position","Value":"posit"},{"Key":"Area","Value":"area"},{"Key":"Area","Value":"area"}]}
,"htmled":{"Name":"htmled","Label":null,"TypeOfControl":2,"Width":170,"DataType":0,"ControlAccessor":null,"ControlType":null,"Items":[]}}";

When I deserialize it, ALMOST everything is correct.当我反序列化它时,几乎一切都是正确的。 The problem is the nested lists.问题是嵌套列表。 While they serialize fine, the list is empty when I put it back to together.虽然它们可以很好地序列化,但当我将其重新组合在一起时,列表是空的。

Any suggestions?有什么建议么?

I assume you are using Json.net I encouter the same problem at work, the deserialization of the lists was incomplete.我假设您使用的是 Json.net 我在工作中遇到了同样的问题,列表的反序列化不完整。 This bug has been added to the issue tracker of the project.此错误已添加到项目的问题跟踪器中。 The workaround we found is to not put the list as the last var in an object.我们找到的解决方法是不要将列表作为 object 中的最后一个变量。

Hope this can help you while waiting for a patch.希望这可以在等待补丁时对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM