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.
它不应该显示具有null值的class属性,而且我正在使用c#.net框架。
5 个解决方案
#1
14
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全局设置它
#2
7
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; }
#3
3
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);
资源
#4
1
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});
#5
1
Like p.s.w.g said, best to serialize properly in the first place. An example of serializing without nulls can be seen here
像p.s.w.g所说的那样,最好先将序列化正确。这里可以看到没有空值的序列化示例
e.g.
例如
var json = JsonConvert.SerializeObject(
objectToSerialize,
new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
#1
14
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全局设置它
#2
7
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; }
#3
3
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);
资源
#4
1
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});
#5
1
Like p.s.w.g said, best to serialize properly in the first place. An example of serializing without nulls can be seen here
像p.s.w.g所说的那样,最好先将序列化正确。这里可以看到没有空值的序列化示例
e.g.
例如
var json = JsonConvert.SerializeObject(
objectToSerialize,
new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});