简体   繁体   English

JSON反序列化后的空子数据结构

[英]Null child data structures after JSON deserialization

I have an object that has a property which is a List of other custom objects. 我有一个对象,该对象的属性是其他自定义对象的列表。 It actually drills down many tiers of custom object properties, but let's just look at the last 3 levels of the object. 它实际上钻取了自定义对象属性的许多层,但让我们仅看一下对象的最后3个级别。

{
    "version" : {
        "version_data" : [ {
            "version_value" : "6.06"
        }, {
            "version_value" : "7.10"
        }, {
            "version_value" : "8.04"
        }, {
            "version_value" : "8.10"
        } ]
    }
}

So my classes look like so: 所以我的课程看起来像这样:

public class Version
{
    [Required()]
    public Version_Data version_data { get; set; }
}
public class Version_Data
{
    [MinLength(1)]
    public List<Version_Data_Item> items { get; set; }
}
public class Version_Data_Item
{
    [Required()]
    public string version_value { get; set; }
}

I was able to extract one version_data_item 我能够提取一个version_data_item

{
     "version_value" : "6.06"
}

and if I deserialize that JSON I am able to call something like: deserializedObject.version_value and it returns 6.06 如果我反序列化该JSON,则可以调用以下代码: deserializedObject.version_value ,它返回6.06

The problem I run into is the upper levels. 我遇到的问题是高层。 For example, I then extract the "Version_Data" object from the data 例如,然后从数据中提取“ Version_Data”对象

{
    "version_data" : [ {
        "version_value" : "6.06"
    }, {
        "version_value" : "7.10"
    }, {
        "version_value" : "8.04"
    }, {
        "version_value" : "8.10"
    } ]
}

that deserializes into a Version_Data (no exceptions raised at least), but when I debug it says the list is null . 反序列化为Version_Data(至少没有引发异常),但是当我调试它时,该列表为null

在此处输入图片说明

What am I missing? 我想念什么?

Your version_data should be called items: 您的version_data应该称为items:

{
    "items" : [ {
        "version_value" : "6.06"
    }, {
        "version_value" : "7.10"
    }, {
        "version_value" : "8.04"
    }, {
        "version_value" : "8.10"
    } ]
}

For generation C# from JSON you can use very helpful web generator . 为了从JSON生成C#,您可以使用非常有用的Web 生成器

Your correct models are here: 您的正确模型在这里:

public class VersionData
{
    public string version_value { get; set; }
}

public class Version
{
    public List<VersionData> version_data { get; set; }
}

public class RootObject
{
    public Version version { get; set; }
}

Are you using Newtonsoft's JSON? 您正在使用Newtonsoft的JSON吗? The structure does not look correct. 该结构看起来不正确。 Unless you use custom parser, the structure should be like this: https://app.quicktype.io?share=Olvtc5B3C1zg7hy2CJgp 除非您使用自定义解析器,否则结构应如下所示: https : //app.quicktype.io?share=Olvtc5B3C1zg7hy2CJgp

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

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