Newtonsoft.Json自定义序列化字段

来源:建筑界编辑:黄子俊发布时间:2020-03-22 19:39:04

[摘要] Revit二次开发:Newtonsoft Json自定义序列化字段class Program{static void Main(string[] args){var m = new JsonModel()

Revit二次开发:Newtonsoft.Json自定义序列化字段

class Program

{

static void Main(string[] args)

{

var m = new JsonModel()

{

Id = 0,

Name = "nnnn"

};

var json = JsonConvert.SerializeObject(m, new JsonSerializerSettings()

{

ContractResolver = new MyContractResolver(new List(){ "Id" })

});

Console.WriteLine(json);

}

}

public class JsonModel

{

[JsonProperty("myId")]

public int Id { get; set; }

public string Name { get; set; }

}

public class MyContractResolver : DefaultContractResolver

{

private List _names = null;

public MyContractResolver(List names)

{

_names = names;

}

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)

{

string name = member.Name;

if (_names.Contains(name))

return base.CreateProperty(member, memberSerialization);

return null;

}

}

自定义,字段,序列化

延展阅读

相关文章