【问题标题】:Skip empty collections when serializing to JSON [closed]序列化为 JSON 时跳过空集合 [关闭]
【发布时间】:2021-08-25 15:41:59
【问题描述】:

我正在开发一个 C# 项目。以下是我的 JSON。我想从中删除“类型”:“产品”的“项目”:[],因为它会导致用户界面出现问题。在创建 JSON 时,我尝试使用 How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net? 中描述的以下 Item 类来排除该值。但它没有工作,不知道为什么。或者,有没有办法在创建 JSON 后将其删除?

  [
     {
      "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);
    }

【问题讨论】:

  • 所以你不想想修改JSON字符串,你想跳过空数组
  • 我想通过删除 "Type":"Product" 下的 "items":[ ] 来修改 JSON 字符串。
  • 不应该是return this.Type != "Product";吗?
  • 是的,让我更正一下。我之前也试过。
  • 我试图仅排除 public IList items { get;放; } 对应于 ShouldSerializeitems

标签: c# json json.net


【解决方案1】:

在序列化后从 JSON 字符串中删除数据几乎总是错误的方法。您应该始终在序列化期间处理它。

此外,您很可能应该检查 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 items.Any();
    }

    public Item()
    {
        items = new List<Item>();
    }
}

这正确输出:

[
  {
    "Type": "Category",
    "Order": 1,
    "id": 24,
    "text": "abc",
    "items": [
      {
        "Type": "Product",
        "Order": 0,
        "id": 1900,
        "text": "abc product"
      }
    ]
  },
  {
    "Type": "Category",
    "Order": 1,
    "id": 6,
    "text": "efg",
    "items": [
      {
        "Type": "Product",
        "Order": 0,
        "id": 2446,
        "text": "efg Product"
      },
      {
        "Type": "Product",
        "Order": 0,
        "id": 2447,
        "text": "efg1 Product"
      }
    ]
  }
]

【讨论】:

  • 嗨大卫,我已经尝试了你的解决方案,仍然“项目”:[]包含在“产品”下。实际上,我已经复制了您的 Item 类并在我的代码中使用它以避免任何拼写错误。
  • 你是如何序列化数据的?我拿了你的确切 json,将它反序列化为一个 Item 列表,然后重新序列化它,输出工作得很好。如果这对您不起作用,那么您一定是在做您未在问题中突出显示的其他事情。
  • @Kim - 你确定你使用的是json.net 而不是system.text.json?您已标记您的问题json.net,此答案将适用于该序列化程序,但是如果您使用的是system.text.json,则不支持条件序列化。如果此答案不起作用,请edit您的原始问题提供minimal reproducible example。
  • dbc,你是对的。我正在使用 System.Web.Script.Serialization.JavaScriptSerializer().Serialize(categoryData);因为失误。一旦我将其更改为 JsonConvert.SerializeObject(categoryData);它工作正常。对不起,我的疏忽!
  • 大卫,您的解决方案工作正常我使用了错误的序列化程序。请看我上面的评论。谢谢!
猜你喜欢
  • 2013-08-30
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多