下載app免費領(lǐng)取會員
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<string>(){ "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<string> _names = null;
public MyContractResolver(List<string> 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;
}
}
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。