C# 从JSON中删除具有空字符串值的属性
我有以下JSON,其中一些属性有空字符串,即“”或空值C# 从JSON中删除具有空字符串值的属性,c#,json,C#,Json,我有以下JSON,其中一些属性有空字符串,即“”或空值 { "allOrNone":false, "records":[ { "Address__c":"Street", "ConsentToComm__c":"", "EmailCLDate__c":"", "attributes":{ "type":"Stage_FF_Hot_Alerts__c" } } ] } 我必须从这个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"
}
}
]
}
这可能会有所帮助
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 }
},
};
}
}
namespace-JSON
{
使用制度;
使用System.Collections.Generic;
利用制度全球化;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Converters;
公共部分类根
{
[默认值(“”)
[JsonProperty(“AllorOne”)]
公共布尔AllOrNone{get;set;}
[默认值(“”)
[JsonProperty(“记录”)]
公共记录[]记录{get;set;}
}
公开部分班级记录
{
[默认值(“”)
[JsonProperty(“地址”)]
公共字符串地址c{get;set;}
[默认值(“”)
[JsonProperty(“同意交易”)]
公共字符串approvitocommc{get;set;}
[默认值(“”)
[JsonProperty(“EmailCLDate\uuu c”)]
公共字符串EmailClDateC{get;set;}
[默认值(“”)
[JsonProperty(“属性”)]
公共属性{get;set;}
}
公共部分类属性
{
[默认值(“”)
[JsonProperty(“类型”)]
公共字符串类型{get;set;}
}
公共部分类根
{
publicstaticroot FromJson(stringJSON)=>JsonConvert.DeserializeObject(json,QuickType.Converter.Settings);
}
公共静态类序列化
{
公共静态字符串ToJson(此根self)=>JsonConvert.SerializeObject(self,QuickType.Converter.Settings);
}
内部静态类转换器
{
公共静态只读JsonSerializerSettings设置=新JsonSerializerSettings
{
NullValueHandling=NullValueHandling.Ignore,
ContractResolver=ShouldSerializeContractResolver.Instance,
MetadataPropertyHandling=MetadataPropertyHandling.Ignore,
DateParseHandling=DateParseHandling.None,
转换器={
新的IsoDateTimeConverter{DateTimeStyles=DateTimeStyles.AssumeUniversal}
},
};
}
}
我已经解决了这个问题。我在序列化过程中删除了空值
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();
var temp=JArray.Parse(JSONstring);
临时后代()
第()类
.Where(attr=>attr.Value.ToString()=“”)
.ToList()//您应该调用ToList,因为您将要更改结果,如果结果是IEnumerable,则不可能更改
.ForEach(attr=>attr.Remove());//删除不需要的属性
JSONstring=temp.ToString();
需要说明的是:在这个场景中,您只需要处理要后期处理的JSON—它没有绑定到要序列化的特定对象模型?(这两种方法都是可能的,但有非常不同的实现)是的,我想处理JSON。我从数据库中读取数据并将其存储在DataTable中,然后对该数据表进行序列化。那么,是什么阻止了您?如何删除具有空字符串值的属性,即“”。我不想在JSON中使用这些属性。要了解更多信息,您可以看到输入JSON和我期望作为输出的JSON。@Izzy因为这不是一个序列化场景,我不认为appliesI不能创建任何新类。是的@er mfahhgkt然后在答案左侧标记绿色,否则人们可能会努力为您提供解决方案。这是我自己的答案,我可以在两天后标记@er mfahhgk