C# 如何删除json字符串中的空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15574506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove null value in json string
提问by user2199888
Hi I'm using the below class
嗨,我正在使用以下课程
Public List<string> name;
Public List<string> midname;
Once I serialize it I'm getting the following output like
一旦我序列化它,我就会得到以下输出
{"name":[hari],"midname":null}
But I want my answer to be like this
但我希望我的答案是这样的
{"name":[hari]}
It shouldn't display the class attribute that has null value and I'm using c# .net framework.
它不应该显示具有空值的类属性,我使用的是 c# .net 框架。
回答by p.s.w.g
The full answer depends on how you're serializing your class.
完整的答案取决于您如何序列化您的课程。
If you're using data contracts to serialize your classes, set EmitDefaultValue = false
如果您使用数据协定来序列化您的类,请设置 EmitDefaultValue = false
[DataContract]
class MyClass
{
[DataMember(EmitDefaultValue = false)]
public List<string> name;
[DataMember(EmitDefaultValue = false)]
public List<string> midname { get; set; }
}
If you're using Json.Net, try this instead
如果你使用的是Json.Net,试试这个
class MyClass
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<string> name;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<string> midname { get; set; }
}
Or set it globally with JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore
或者全局设置 JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore
回答by Sachin
If you are using Json.Net
then You can try this by Decorating your property like this
如果您正在使用,Json.Net
那么您可以通过像这样装饰您的财产来尝试这个
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<string> name { get; set; }
回答by tam tam
private class NullPropertiesConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var jsonExample = new Dictionary<string, object>();
foreach (var prop in obj.GetType().GetProperties())
{
//check if decorated with ScriptIgnore attribute
bool ignoreProp = prop.IsDefined(typeof(ScriptIgnoreAttribute), true);
var value = prop.GetValue(obj, BindingFlags.Public, null, null, null);
if (value != null && !ignoreProp)
jsonExample.Add(prop.Name, value);
}
return jsonExample;
}
public override IEnumerable<Type> SupportedTypes
{
get { return GetType().Assembly.GetTypes(); }
}
}
The following is how you will utilize the above and it will ignore null values.
以下是您将如何利用上述内容,它将忽略空值。
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new NullPropertiesConverter() });
return serializer.Serialize(someObjectToSerialize);
回答by mehman abasov
string signatureJson = JsonConvert.SerializeObject(signature, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
string signatureJson = JsonConvert.SerializeObject(signature, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
回答by Daniel Mohr
Like p.s.w.g said, best to serialize properly in the first place. An example of serializing without nulls can be seen here
就像pswg所说的那样,最好首先正确序列化。可以在此处看到没有空值的序列化示例
e.g.
例如
var json = JsonConvert.SerializeObject(
objectToSerialize,
new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});