如何使用json.net忽略類中的屬性(如果為NULL)我在用Json.NET將類序列化為JSON。我有這樣的課:class Test1{
[JsonProperty("id")]
public string ID { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("url")]
public string URL { get; set; }
[JsonProperty("item")]
public List<Test2> Test2List { get; set; }}我想添加一個JsonIgnore()屬性為Test2List屬性僅在Test2List是null..如果它不是空的,那么我想將它包含在我的json中。
3 回答

慕標琳琳
TA貢獻1830條經(jīng)驗 獲得超9個贊
NullValueHandling
JsonSerializer _jsonWriter = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
JsonConvert.SerializeObject(myObject, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

長風秋雁
TA貢獻1757條經(jīng)驗 獲得超7個贊
JsonProperty
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]// or[JsonProperty("property_name", NullValueHandling=NullValueHandling.Ignore)] // or for all properties in a class[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
- 3 回答
- 0 關(guān)注
- 1772 瀏覽
添加回答
舉報
0/150
提交
取消