使用不同的属性名称反序列化嵌套的json

时间:2018-07-09 04:03:33

标签: 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?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);
    }
}

在不使用JsonProperty和其他标签污染模型的情况下,如何使嵌套的company_info正确绑定?当我使用JsonConvert反序列化对象时,我这样传递属性映射:

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"}
    };

Id和Name正确绑定,但是没有任何Info映射。

2 个答案:

答案 0 :(得分:1)

如果您不想更改任何属性名称以与Json属性匹配并且不想使用JsonProperty,则只需将错误的属性映射对此进行修复

private static readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
    {
        {"Id", "record_id"},
        {"Name", "company"},
        {"Info", "company_info"},
        {"State", "location_state"}
    };

只需告诉Deserializer将名称映射为名称,无需在其上放置任何嵌套对象 希望它能解决您的问题

答案 1 :(得分:0)

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

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; }
}

映射可以是:

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"}
    };
相关问题