Newtonsoft JSON 反序列化为列表中的对象列表



我必须将 json 字符串反序列化为我自己的对象,该对象有一个子项列表,这也有一个子项列表。喜欢这个:父类 -> 列表 -> 列表

如何反序列化此 json 字符串?

JSON 示例:

{
{
    "Departementstructure": {
        "org.MainDepartments": [{
            "@MainDepartmentsId": "4",
            "@MainDepartmentsName": "Direktorium",
            "@MainDepartmentsName_En": "Board of Directors",
            "org.WorkAreas": [{
                "@Id": "30",
                "@Name": "Direktorin",
                "@WorkAreasName_En": "Director",
                "org.LIfBiDepartments": {
                    "@LIfBiDepartmentsId": "1",
                    "@LIfBiDepartmentsName": "Direktorin",
                    "@LIfBiDepartmentsName_En": "Director"
                }
            },
            {
                "@Id": "31",
                "@Name": "Kaufmännischer Geschäftsführer",
                "@WorkAreasName_En": "Executive Director of Administration",
                "org.LIfBiDepartments": {
                    "@LIfBiDepartmentsId": "52",
                    "@LIfBiDepartmentsName": "",
                    "@LIfBiDepartmentsName_En": ""
                }
            },
            {
                "@Id": "32",
                "@Name": "Wissenschaftliches Referat des Direktoriums",
                "@WorkAreasName_En": "Scientific Office to the Board of Directors",
                "org.LIfBiDepartments": {
                    "@LIfBiDepartmentsId": "53",
                    "@LIfBiDepartmentsName": "",
                    "@LIfBiDepartmentsName_En": ""
                }
            },..

json属性属性来自尝试解决方案。

   public class LomVereinsstructure
{
    public LomVereinsstructure()
    {
        Parents = new List<LomParentMainDepartment>();
    }
    [JsonProperty("Departementstructure")]
    public List<LomParentMainDepartment> Parents { get; set; }
}
[JsonArray("org.MainDepartments")]
public class LomParentMainDepartment
{
    public LomParentMainDepartment()
    {
        Children = new List<LomChildWorkarea>();
    }
    [DataMember]
    [JsonProperty("@MainDepartmentsId")]
    public int Id { get; set; }
    [DataMember]
    public string ShortName { get; set; }
    [DataMember]
    [JsonProperty("@MainDepartmentsName")]
    public string Name { get; set; }
    [DataMember]
    [JsonProperty("@MainDepartmentsName_En")]
    public string NameEn { get; set; }
    [DataMember]
    //[JsonProperty("org.WorkAreas")]
    public List<LomChildWorkarea> Children { get; set; }
}
[JsonArray("org.WorkAreas")]
public class LomChildWorkarea
{
    public LomChildWorkarea()
    {
        Children = new List<LomChildDepartment>();
    }
    [DataMember]
    [JsonProperty("@Id")]
    public int Id { get; set; }
    [DataMember]
    public string ShortName { get; set; }
    [DataMember]
    [JsonProperty("@Name")]
    public string Name { get; set; }
    [DataMember]
    [JsonProperty("@WorkAreasName_En")]
    public string NameEn { get; set; }
    [DataMember]
    //[JsonProperty("@org.LIfBiDepartments")]
    public List<LomChildDepartment> Children { get; set; }
}
[JsonArray("org.LIfBiDepartments")]
public class LomChildDepartment
{
    public LomChildDepartment()
    {
        OrganisationUnit = new LomChildOrganisationUnit();
    }
    [DataMember]
    [JsonProperty("@LIfBiDepartmentsId")]
    public int Id { get; set; }
    [DataMember]
    public string ShortName { get; set; }
    [DataMember]
    [JsonProperty("@LIfBiDepartmentsName")]
    public string Name { get; set; }
    [DataMember]
    [JsonProperty("@LIfBiDepartmentsName_En")]
    public string NameEn { get; set; }
    [DataMember]
    public LomChildOrganisationUnit OrganisationUnit { get; set; }
}

非常感谢您的帮助

若要轻松生成可以保存 JSON 数据的类,可以使用 JSON-C# 转换服务之一,如 https://jsonutils.com/或 http://json2csharp.com/。如果这些无法生成类,您应该使用像 https://jsonlint.com/这样的验证器来查看 JSON 是否确实有效(从代码片段来看,如果它实际上是一个数组,则第一个大括号应该是正方形的(。

这里有一个差异:

在您的 json 字符串中,您有一个 org.LIfBiDepartments 而不是数组

的对象

在你的班级模型中,你有一个public List<LomChildDepartment> Children { get; set; }

因此,只需修复您的 json 字符串以表示数组或修复您的模型以仅接受 1 个对象。

在任何情况下,使用 Visual Studio 中的解析器来帮助你查看它为 json 字符串重现的类模型。非常方便。

public class Departement
{
    [JsonProperty("org.MainDepartments")]
    public IList<Departaments> MainDepartments { get; set; }
}
public class Departaments
{
    [JsonProperty("@MainDepartmentsId")]
    public string DpId { get; set; }
    [JsonProperty("@MainDepartmentsName")]
    public string DpName { get; set; }
    [JsonProperty("@MainDepartmentsName_En")]
    public string DpNameEn { get; set; }
    [JsonProperty("org.WorkAreas")]
    public IList<WorkAreas> WorkAreas { get; set; }
}
public class WorkAreas
{
    [JsonProperty("@Id")]
    public string WorkId { get; set; }
    [JsonProperty("@Name")]
    public string WorkName { get; set; }
    [JsonProperty("@WorkAreasName_En")]
    public string WorkNameEn { get; set; }
    [JsonProperty("org.LIfBiDepartments")]
    public LIfBiDepartments LIfBiDepartments { get; set; }
}
public class LIfBiDepartments
{
    [JsonProperty("@LIfBiDepartmentsId")]
    public string BiDpId { get; set; }
    [JsonProperty("@LIfBiDepartmentsName")]
    public string BiDpName { get; set; }
    [JsonProperty("@LIfBiDepartmentsName_En")]
    public string BiDpNameEn { get; set; }
}
Use a property in the parent class to call the object's deserializer in 
daughters classes. 
Example:
    [JsonProperty("Departementstructure")]
    public Departement MainDepartement { get; set; }
Output:

在此处输入图像描述

Visual Studio有一个解析器来分类编辑->粘贴特殊->过去的JSON作为类,然后你可以将它与你自己的对象进行比较,看看有什么问题 - Zorkind

我通过使用Visual Studio的集成解析器解决了这个问题。

谢谢

相关内容

  • 没有找到相关文章

最新更新