Giter Club home page Giter Club logo

javabean2json's Introduction

title date tags categories comments toc
Java对象转json时空值(null)处理
2018-12-20 01:33:23 -0800
Java
Json
Json
true
true

Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?

1 fastjson

fastJson在转换java对象为json的时候,默认是不序列化null值对应的key的也就是说当对象里面的属性为空的时候,在转换成json时,不序列化那些为null值的属性

package top.sogrey.java2Json;

import top.sogrey.java2Json.bean.User;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * fastjson 版本是 1.2.54
 * 
 * @author Administrator
 * 
 */
public class fastjsonDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		User user = new User();
		user.setUserName("Sogrey");
		String str = JSONObject.toJSONString(user);// fastjson默认转换是不序列化null值对应的key的
		System.out.println(str);
		// 输出结果是:{"age":0,"userName":"Sogrey"}
	}
}

但是如果想把null对应的key序列化出来呢?

		// 如果需要序列化null对应的key
		str = JSONObject
				.toJSONString(
						user,
						new SerializerFeature[] { SerializerFeature.WriteMapNullValue });
		System.out.println(str);
		// 输出结果是: {"age":0,"job":null,"userName":"Sogrey"}

想字符类型字段如果为null,转换输出为”“,而非null ,需要多加一个参数:WriteNullStringAsEmpty

		// 想字符类型字段如果为null,转换输出为"",而非null ,需要多加一个参数:WriteNullStringAsEmpty
		str = JSONObject.toJSONString(user, new SerializerFeature[] {
				SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteNullStringAsEmpty });
		System.out.println(str);
		//输出结果是:{"age":0,"job":"","userName":"Sogrey"}

2 Gson

gson和fastjson一样,默认是不序列化null值对应的key的

package top.sogrey.java2Json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import top.sogrey.java2Json.bean.User;

public class GsonDemo {

   /**
    * @param args
    */
   public static void main(String[] args) {
   	User user = new User();
   	user.setUserName("Sogrey");
   	Gson g = new GsonBuilder().create();
   	String str = g.toJson(user);
   	System.out.println(str);
   	// {"userName":"Sogrey","age":0}
   }
}

若是想序列化null值对应的key,只需要将以上创建代码改成以下代码就行:

   	g = new GsonBuilder().serializeNulls().create();
   	str = g.toJson(user);
   	System.out.println(str);
   	// {"userName":"Sogrey","age":0,"job":null}

javabean2json's People

Contributors

sogrey avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.