Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化具有不同属性名称的嵌套json_C#_Json.net - Fatal编程技术网

C# 反序列化具有不同属性名称的嵌套json

C# 反序列化具有不同属性名称的嵌套json,c#,json.net,C#,Json.net,我有一个自定义契约解析器,可以将模型中的属性名称解析为RESTful API调用返回的属性名称。这是一个示例API调用: { "record_id": "f461dc88-2a3c-428c-ad92-1d03477667cf", "company": "Palm Beach Photographics", "company_info": { "images": [ "https://picsum.photos/200/300?im

我有一个自定义契约解析器,可以将模型中的属性名称解析为RESTful API调用返回的属性名称。这是一个示例API调用:

{
    "record_id": "f461dc88-2a3c-428c-ad92-1d03477667cf",
    "company": "Palm Beach Photographics",
    "company_info": {
        "images": [
            "https://picsum.photos/200/300?image=719",
            "https://picsum.photos/200/300?image=653",
            "https://picsum.photos/200/300?image=965"
        ],
        "industries": [
            "Real Estate",
            "Photo/Film",
            "Utilities/Energy/Infrastructure",
            "Emergency Services"
        ],
        "location_state": "FL",
        "overview": "Per fusce luctus diam hac dictum posuere non interdum conubia eleifend, fusce suscipit purus aliquam porttitor amet fames nostra sapien potenti fusce, quisque felis quisque ante fringilla nulla rutrum porta at inceptos mi per mattis rhoncus id."
    }
}
我的模型如下:

public class Company
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public CompanyInfo Info { get; set; }
}

public class CompanyInfo
{
    public IEnumerable<Uri> Images { get; set; }

    public string State { get; set; }

    public IEnumerable<string> Industries { get; set; }

    public string Overview { get; set; }
}

public class CustomContractResolver : DefaultContractResolver
{

    private Dictionary<string, string> PropertyMappings { get; }

    public CustomContractResolver(Dictionary<string, string> propertyMappings)
    {
        PropertyMappings = propertyMappings;
    }

    protected override string ResolvePropertyName(string propertyName)
    {
        var resolved = PropertyMappings.TryGetValue(propertyName, out var resolvedName);
        return (resolved) ? resolvedName : base.ResolvePropertyName(propertyName);
    }
}

Id和名称正确绑定,但没有任何信息映射

您要求的示例,格式如下:

public class Company
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public CompanyInfo Info { get; set; }

    public IEnumerable<Uri> Info_Images { 
      get {
         return Info.Images;
      }
      set {
         if (Info == null) {
            Info = new CompanyInfo();
         }
         Info.Images = value 
      }
    }
    // etc., with the other properties.
}

public class CompanyInfo
{
    public IEnumerable<Uri> Images { get; set; }

    public string State { get; set; }

    public IEnumerable<string> Industries { get; set; }

    public string Overview { get; set; }
}
上市公司
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共公司信息{get;set;}
公共IEnumerable Info_图像{
得到{
返回信息。图像;
}
设置{
if(Info==null){
Info=新公司Info();
}
Info.Images=value
}
}
//等,与其他属性。
}
公共类公司信息
{
公共IEnumerable映像{get;set;}
公共字符串状态{get;set;}
公共IEnumerable工业{get;set;}
公共字符串概述{get;set;}
}
映射可以如下所示:

private readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
    {
        {"Id", "record_id"},
        {"Name", "company"},
        {"Info_Images", "company_info.images"},
        {"Info_Industries", "company_info.industries"},
        {"Info_State", "company_info.location_state"},
        {"Info_Overview", "company_info.overview"}
    };
private readonly Dictionary\u propertyMappings=新字典
{
{“Id”,“record_Id”},
{“名称”,“公司”},
{“信息图片”,“公司信息图片”},
{“信息产业”、“公司信息产业”},
{“信息状态”、“公司信息位置状态”},
{“信息概述”、“公司信息概述”}
};

如果您不想更改任何与Json属性匹配的属性名称,也不想使用JsonProperty,您可以修复错误的属性映射到此属性

private static readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
    {
        {"Id", "record_id"},
        {"Name", "company"},
        {"Info", "company_info"},
        {"State", "location_state"}
    };
private static readonly Dictionary\u propertyMappings=new Dictionary
{
{“Id”,“record_Id”},
{“名称”,“公司”},
{“信息”,“公司信息”},
{“状态”,“位置\状态”}
};
只需告诉反序列化程序将名称映射到名称,无需在其上放置任何嵌套对象
希望它能解决您的问题

映射程序使用反射获取属性名称。您的符号当前未映射到任何现有属性。让您的成员对象的属性直接解析为成员对象。对不起,您是否建议我将公司信息的属性移动到公司中?请你举例说明你的意思,或者再详细说明一下。啊,太好了。成功了。我很好奇这个决议是如何运作的。当合同解析器点击公司信息时,我想象它开始将信息映射到公司信息下的所有属性。之后如何将状态映射到location\u State?我认为在嵌套类中,当试图解析
location\u State
时,它们与根类做的相同,它在自定义映射中检查
location\u State
是否存在于映射中,如果它们不是使用相同的名称映射,但如果存在,则使用我们指定的特定名称映射请注意,这种方法打开了一扇通向各种歧义的大门,当底层对象发生任何更改时,这些歧义可能会使调试变得麻烦。如果您为反序列化创建自定义映射,并且在不更改映射的情况下更改类,则每种方法都会使调试变得麻烦
private static readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
    {
        {"Id", "record_id"},
        {"Name", "company"},
        {"Info", "company_info"},
        {"State", "location_state"}
    };