【问题标题】:Remove attributes from JSON which have empty string value从 JSON 中删除具有空字符串值的属性
【发布时间】:2023-04-07 02:33:01
【问题描述】:

我有以下 JSON,其中某些属性具有空字符串,即“”或空值

{  
   "allOrNone":false,
   "records":[  
  {  
     "Address__c":"Street",
     "ConsentToComm__c":"",
     "EmailCLDate__c":"",
     "attributes":{  
        "type":"Stage_FF_Hot_Alerts__c"
     }
  }
 ]
}

我必须从这个 JSON 中删除空字符串和空值属性。我怎样才能删除它们。我在 C# 中这样做。删除空字符串和 null 后所需的 JSON 将是:

{  
   "allOrNone":false,
   "records":[  
  {  
     "Address__c":"Street",
     "attributes":{  
        "type":"Stage_FF_Hot_Alerts__c"
     }
  }
 ]
}

【问题讨论】:

  • 要明确:在这种情况下,您只是拥有要进行后处理的 JSON - 它与您正在序列化的特定对象模型无关? (两种方式都可以,但实现方式大不相同)
  • 是的,我想处理 JSON。我从数据库中读取数据并将其存储在 DataTable 中,然后将这个数据表序列化。
  • 那么,是什么阻止了你?
  • 如何删除具有空字符串值的属性,即“”。我不希望我的 JSON 中有这些属性。如需更多说明,您可以查看输入 JSON 和我期望作为输出的 JSON。
  • @Izzy 因为这不是序列化场景,我认为这不适用

标签: c# json


【解决方案1】:

我已经解决了这个问题。我在序列化过程中删除了空值。

string JSONstring = JsonConvert.SerializeObject(dt, new 
JsonSerializerSettings()
{
            NullValueHandling = NullValueHandling.Ignore,
});

然后通过以下代码删除空字符串值

var temp = JArray.Parse(JSONstring);
temp.Descendants()
    .OfType<JProperty>()
    .Where(attr => attr.Value.ToString() == "")
    .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
    .ForEach(attr => attr.Remove()); // removing unwanted attributes

JSONstring = temp.ToString();

【讨论】:

  • 是的@er-mfahhgk
  • 然后标记答案左侧的勾号使其变为绿色,否则人们可能会努力为您提供解决方案。
  • 这是我自己的答案,我可以在 2 天后标记它@er-mfahhgk
【解决方案2】:

这可能会有所帮助

namespace JSON
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Root
    {
        [DefaultValue("")]
        [JsonProperty("allOrNone")]
        public bool AllOrNone { get; set; }

        [DefaultValue("")]
        [JsonProperty("records")]
        public Record[] Records { get; set; }
    }

    public partial class Record
    {
        [DefaultValue("")]
        [JsonProperty("Address__c")]
        public string AddressC { get; set; }

        [DefaultValue("")]
        [JsonProperty("ConsentToComm__c")]
        public string ConsentToCommC { get; set; }

        [DefaultValue("")]
        [JsonProperty("EmailCLDate__c")]
        public string EmailClDateC { get; set; }

        [DefaultValue("")]
        [JsonProperty("attributes")]
        public Attributes Attributes { get; set; }
    }

    public partial class Attributes
    {
        [DefaultValue("")]
        [JsonProperty("type")]
        public string Type { get; set; }
    }

    public partial class Root
    {
        public static Root FromJson(string json) => JsonConvert.DeserializeObject<Root>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Root self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            ContractResolver = ShouldSerializeContractResolver.Instance,
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}

【讨论】:

  • 我无法开设任何新课程。
猜你喜欢
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 2017-11-29
  • 2015-11-09
相关资源
最近更新 更多