0

I'm sending json format and I would like to ask if it's possible to remove a sublist from a list. I have models like these.

public class lineItems
{
     public string item_name { get; set; }
     public int quantity { get; set; }
     public string price { get; set; }
     public List<subItem> subItem { get; set; }
}
public class subItem
{
     public string sub_item_name { get; set; }
     public int quantity { get; set; }
}

The json looks like this. What i'm trying to do is remove the subItem from the second item. I tried to make the subItem null but it still displays in the json format with "subItem": [].

https://i.stack.imgur.com/4Tlim.png

Thank you.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
Mina
  • 1
  • 1
    Does this answer your question - https://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net ? – dimitar.d Jul 30 '20 at 10:00
  • Thank you so much @dimitar.d .I will try this. – Mina Jul 30 '20 at 10:02
  • Hi. I tried this code JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; var json = JsonConvert.SerializeObject(order, settings); it removes the null items but it seems the the subItem still cannot be removed from the json. { "item_name ": "sub_item_2", "quantity": 1, "price": "84000.00", "subItem": [ null ] } – Mina Jul 30 '20 at 11:32
  • Thank you so much. The link above works for me. – Mina Jul 30 '20 at 23:57

0 Answers0