使用jackson解析JSON数据

本文介绍使用jackson来对json数据进行解析操作

首先,需要去官网下载jackson,本文使用的是(jackson-all-1.9.11.jar)

主要通过ObjectMapper对json进行处理

 package com.whroid.java.json;

 import java.io.IOException;

 import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig; public class JacksonUtil { ObjectMapper mObjectMapper;
JsonFactory mJsonFactory;
static JacksonUtil mJacksonUtil;
public static JacksonUtil getInstance()
{
if(mJacksonUtil == null)
{
mJacksonUtil = new JacksonUtil();
}
return mJacksonUtil;
}
private JacksonUtil()
{
mObjectMapper = new ObjectMapper();
mObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);//(禁止SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)
mJsonFactory = mObjectMapper.getJsonFactory();
} /**
* 将json字符串数据,转换成cls类实例。需要保证cls类中的属性和json中相对应,并且cls中必须有相应的getter()/setter()方法
* @param json
* @param cls
* @return
* @throws JsonParseException
* @throws IOException
*/
public <T extends Object> T jsonToObject(String json,Class<T> cls) throws JsonParseException, IOException
{
return mObjectMapper.readValue(json, cls);
}
/**
* 将实例对象,转化成json字符串。需要保证cls类中的属性和json中相对应,并且cls中必须有相应的getter()/setter()方法
* @param obj
* @return
* @throws JsonGenerationException
* @throws JsonMappingException
* @throws IOException
*/
public String objectToJson(Object obj) throws JsonGenerationException, JsonMappingException, IOException
{
return mObjectMapper.writeValueAsString(obj);
}
}

与json转化的对象类JSONBean,需要添加getter()/setter()方法

 package com.whroid.java.json;

 import java.util.ArrayList;
import java.util.List; public class JSONBean { private String name = "name";
private int id = 1;
private List<String> list = new ArrayList<String>();
private boolean b;
SubBean subBean = new SubBean();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public SubBean getSubBean() {
return subBean;
}
public void setSubBean(SubBean subBean) {
this.subBean = subBean;
}
@Override
public String toString() {
return "JSONBean [name=" + name + ", id=" + id + ", list=" + list
+ ", b=" + b + ", subBean=" + subBean + "]";
} }
class SubBean{
String subname = "subname"; public String getSubname() {
return subname;
} public void setSubname(String subname) {
this.subname = subname;
} @Override
public String toString() {
return "SubBean [subname=" + subname + "]";
} }

调用

 public static void main(String[] args) {

         JSONBean bean = new JSONBean();
String jackson = null;
try {
jackson = JacksonUtil.getInstance().objectToJson(bean);
System.out.println("jackson:"+jackson); bean = JacksonUtil.getInstance().jsonToObject(jackson, JSONBean.class);
System.out.println("bean:"+bean);
} catch (Exception e) {
e.printStackTrace();
} }

返回:

jackson:{"name":"name","id":1,"list":[],"b":false,"subBean":{"subname":"subname"}}

//使用上面那段json解析后的对象
bean:JSONBean [name=name, id=1, list=[], b=false, subBean=SubBean [subname=subname]]

上一篇:在java程序中,对于数据的输入/输出操作以“流”(stream)方式进行


下一篇:【转载】Java下利用Jackson进行JSON解析和序列化