Newtonsoft.Json自定义序列化字段

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;

}

}

人已赞赏
BIM操作技巧

Revit中临时尺寸变成永久尺寸

2020-2-5 0:04:59

BIM操作技巧

如何把CAD图纸转化成PS可编辑格式

2020-2-5 0:05:09

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索