使用jackson生成json对象的实例

这里写了一个将json和Java的Object对象进行互相转换的Util工具类。
这里以maven为例,导入jackson的两个dependency。

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.2.3</version>
</dependency>

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-annotations</artifactId>
	<version>2.2.3</version>
</dependency>


接下来是util实例

package com.lj.basic.util;

import java.io.StringWriter;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
	private static JsonUtil jsonUtil;
	private static JsonFactory jsonFactory;
	private static ObjectMapper objectMapper;
	private JsonUtil(){
		
	}
	
	public static JsonUtil getInstance(){
		if(jsonUtil==null) jsonUtil=new JsonUtil();
		return jsonUtil;
	}
	
	public static ObjectMapper getMapper(){
		if(objectMapper == null){
			objectMapper=new ObjectMapper();
		}
		return objectMapper;
	}
	
	public static JsonFactory getFactory(){
		if(jsonFactory==null) jsonFactory=new JsonFactory();
		return jsonFactory;
	}
	
	public String obj2json(Object obj){
		JsonGenerator jsonGenerator = null;
		try {
			jsonFactory=getFactory();
			objectMapper=getMapper();
			StringWriter out = new StringWriter();
			jsonGenerator = jsonFactory.createGenerator(out);
			objectMapper.writeValue(jsonGenerator, obj);
			return out.toString();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				if(jsonGenerator!=null)jsonGenerator.close();
			} catch (Exception e2) {
				 e2.printStackTrace();
			}
		}
		
		return null;
	}

	
	public Object json2obj(String json, Class<?> clz){
		try {
			objectMapper=getMapper();
			return objectMapper.readValue(json, clz);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	

}



你可能感兴趣的:(java,json)