我有一个问题,确实需要一些解决方案。我的课在下面。
public class Attributes
{
public int attributeId { get; set; }
public int ?attributeValueId { get; set; }
public string customAttributeValue { get; set; }
}
public class Products
{
public string barcode { get; set; }
public string title { get; set; }
public string productMainId { get; set; }
public List<Attributes> attributes { get; set; }
}
我不希望它可见。当我无法为customAttributeValue属性分配值时,就会创建Json。每当我得到下面的输出。
每次我得到下面的输出。
"attributes": [
{
"attributeId": 338,
"attributeValueId": 3961,
"customAttributeValue": null
},
{
"attributeId": 343,
"attributeValueId": 4294,
"customAttributeValue": null
},
{
"attributeId": 47,
"attributeValueId": 0,
"customAttributeValue": "Black"
}
]
我需要发送此类型..
"attributes": [
{
"attributeId": 338,
"attributeValueId": 6980
},
{
"attributeId": 47,
"customAttributeValue": "BLACK"
},
{
"attributeId": 346,
**attributeValueId": 4290**
感谢您的帮助。
下面的示例使用JsonIgnoreAttribute从序列化中排除属性。
[JsonIgnore]
public string PasswordHash { get; set; }
https://www.newtonsoft.com/json/help/html/PropertyJsonIgnore.htm
当您只想使用空值时
https://www.newtonsoft.com/json/help/html/NullValueHandlingIgnore.htm
您可以这样操作:
public class Vessel{
public string Name { get; set; }
public string Class { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? LaunchDate { get; set; }}
string json = JsonConvert.SerializeObject(vessel, Formatting.Indented);
和]的输出>
Vessel vessel = new Vessel{ Name = "Red October", Class = "Typhoon"};
将是:
{“名称”:“红色十月”,“类别”:“台风”}