如何在Json.Net中重命名JsonProperty属性类型的子属性?

在使用自定义ContractResolver进行序列化时,我已经实现了用于一级对象属性的东西,如下所示:

protected override IList<JsonProperty> CreateProperties(Type type,MemberSerialization memberSerialization)
{
    var properties = base.CreateProperties(type,memberSerialization);
    foreach (p in properties)
    {
       p.PropertyName = "any_new_name";
    }

    return properties;
}

但是有时候我需要更深入地重命名嵌套属性。怎么可能呢?使用递归CreateProperties

例如,我必须序列化一个对象

public class DTO
{
    public string Foo { get; set; }

    public int Bar1 { get; set; }

    public Bar Bar2 { get; set; }
}

Bar

public class GetReportDataViewModel
{
    public string Foo1 { get; set; }

    public int Bar21 { get; set; }
}

因此,我希望结果类似于

  {
    "any_new_name": "str_value","any_new_name": int_value,"any_new_name": {
       "any_new_name": "str_value","any_new_name": int_value
      }
  }
vighi 回答:如何在Json.Net中重命名JsonProperty属性类型的子属性?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2964955.html

大家都在问