JSON序列化自动过滤NULL值

使用Newtonsoft.Json.dll 序列化为json时主动将NULL值过滤掉,具体做法:

var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore};

var json = JsonConvert.SerializeObject(response, Formatting.Indented, jSetting);

之前转换的JSON为:

{"header":{"responseCode":"200","responseDesc"":"执行成功","responseDescErrorMsg":"","responseDescTime":"2015-07-07T13:40:48.7162196+08:00"},"body":{"order":{"orderid":"S1507070001"},"product":null}}

参加代码后转换出来的为:

{"header":{"responseCode":"200","responseDesc":"执行成功","responseDescErrorMsg":"","responseDescTime":"2015-07-07T13:40:48.7162196+08:00"},"body":{"order":{"orderid":"S1507070001"}}}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 00:47:24

JSON序列化自动过滤NULL值的相关文章

JSON序列化自己主动过滤NULL值

使用Newtonsoft.Json.dll 序列化为json时主动将NULL值过滤掉.详细做法: var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore}; var json = JsonConvert.SerializeObject(response, Formatting.Indented, jSetting); 之前转换的JSON为: {"header":{&q

automapper如何全局配置map条件过滤null值空值对所有映射起效

原文 automapper如何全局配置map条件过滤null值空值对所有映射起效 我们在使用automapper的时候经常会遇到这样的问题:假设展示给用户的数据我们用UserDto类,User类就是我们的实体类.在给用户编辑的时候,我们可能某些字段在数据库中为Null,这时候需要一些默认值 比如这里UserDto中的BirTime,然后我们有一些人的习惯是在构造函数里面进行赋值 public class User { public int Id { get; set; } public stri

jackson 转json. 过滤null值

@Test public void tttttt() throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); // 过滤对象的null属性. mapper.setSerializationInclusion(Inclusion.NON_NULL); // 过滤map中的null值 mapper.configure(Serializat

hive对有null值的列进行avg,sum,count等操作时会不会过滤null值

在hive中,我们经常会遇到对某列进行count.sum.avg等操作计算记录数.求和.求平均值等,但这列经常会出现有null值的情况,那这些操作会不会过滤掉null能呢? 下面我们简单测试下: with tmp as(select null as col1 union allselect 666 as col1 union allselect 999 as col1)select avg(col1) avg_numm, sum(col1) sum_num, count(1) cnt, coun

Spring3.2中返回JSON去掉Bean中NULL值

在Spring3.2中返回Json字符串,过滤掉不需要的Bean中属性对应Json中key的value=null的值,可以同过在Bean中加入注解的方式来过滤 使用jackson-all-1.8.1.jar包,在需要序列化输出Json的类上添加如***解 import org.codehaus.jackson.map.annotate.JsonSerialize; @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) public cl

直接使用提交过来的类来更新字段EntityState.Modified并过滤null值的方法

public T Update<T>(T entity) where T : ModelBase { var set = this.Set<T>(); set.Attach(entity); foreach (System.Reflection.PropertyInfo p in entity.GetType().GetProperties()) { if (p.GetValue(entity) != null) { this.Entry<T>(entity).Prop

json序列化NULL

在项目中遇到一问题,json序列化需要将null传递给前端,但之前项目中使用的都是fastjson的JSONObject.toJSONString(vo),这样会过滤掉为NULL的属性. 解决办法: 1.依然使用fastjson,使用SerializerFeature.WriteMapNullValue参数,不过滤null,null也会传递给前端 String s = JSONObject.toJSONString(vo,SerializerFeature.WriteMapNullValue);

iOS开发——model类模板(过滤null和ID)

        说明:model类模板已默认过滤null值,附加特殊情况的关键字ID名的冲突(需手动去掉注释代码).MyMessageModel为示例的名字.可以自己随便起. 1.自己创建一个继承与NSObject的类,用于当model数据模型用.然后在.h文件中根据接口文档或者json返回数据的添加相应属性.    并复制以下model类模板代码.h文件的- (instancetype)initWithDictionary:(NSDictionary *)dictionary;方法到自己创建的

hive 配置文件以及join中null值的处理

一.Hive的参数设置 1.  三种设定方式:配置文件 ·   用户自定义配置文件:$HIVE_CONF_DIR/hive-site.xml ·   默认配置文件:$HIVE_CONF_DIR/hive-default.xml 用户自定义配置会覆盖默认配置.另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的,Hadoop的配置文件包括 ·   $HADOOP_CONF_DIR/hive-site.xml ·   $HADOOP_CONF_DIR/hive-de