【发布时间】:2019-08-20 13:05:47
【问题描述】:
我们如何隐藏使用 JSON.NET 库进行序列化的 C# 属性。假设,我们有类 Customer
public class Customer
{
public int CustId {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public bool isLocked {get; set;}
public Customer() {}
}
public class Test
{
Customer cust = new Customer();
cust.CustId = 101;
cust.FirstName = "John"
cust.LastName = "Murphy"
string Json = JsonConvert.SerializeObject(cust);
}
JSON
{
"CustId": 101,
"FirstName": "John",
"LastName": "Murphy",
"isLocked": false
}
这个对象被转换成 json,但是我没有指定 isLocked 属性。由于库将序列化整个类,有没有办法在json序列化过程中忽略一个属性,或者我们可以在属性上添加任何属性。
编辑: 此外,如果我们在数组中创建 Customer 类的两个实例。如果我们没有在第二个实例上指定 is locked 属性,我们可以隐藏第二个对象的属性吗?
JSON
{
"Customer": [
{
"CustId": 101,
"FirstName": "John",
"LastName": "Murphy",
"isLocked": false
},
{
"CustId": 102,
"FirstName": "Sara",
"LastName": "connie"
}
]
}
【问题讨论】:
标签: c# visual-studio serialization properties json.net