I am working on a C# project. The following is my JSON. I want to remove the "items":[] from it for "Type":"Product" as it is causing problems on the UI. I tried to exclude the value while JSON is being created by using the following Item class as described at How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net?. But it didn't work, not sure why. Alternatively, is there a way to remove it after the JSON is created?
[
{
"Type":"Category",
"Order":1,
"id":"24",
"text":"abc",
"items":[
{
"Type":"Product",
"Order":0,
"id":1900,
"text":"abc product",
"items":[
]
}
]
},
{
"Type":"Category",
"Order":1,
"id":"6",
"text":"efg",
"items":[
{
"Type":"Product",
"Order":0,
"id":2446,
"text":"efg Product",
"items":[
]
},
{
"Type":"Product",
"Order":0,
"id":2447,
"text":"efg1 Product",
"items":[
]
}
]
}
]
[Serializable]
public class Item
{
public String Type { get; set; }
public int Order { get; set; }
public int id { get; set; }
public string text { get; set; }
public IList<Item> items { get; set; }
public bool ShouldSerializeitems()
{
return this.Type != "Product";
}
public Item()
{
items = new List<Item>();
}
}
foreach (Products product in products)
{
Item product = new Item();
product.id = Convert.ToInt32(product.GetProductID());
product.text = product.GetName();
product.Order = product.GetProductSortOrder();
product.Type = "Product";
Category.items.Add(product);
}