处理json格式数据的几大框架对比 : http://wangym.iteye.com/blog/738933
jackson-all-1.7.4.jar 包下载:http://pan.baidu.com/s/1g6bQe
先将ObjectMapper封装成单例(实际应用中建议这么用)
package com.json.util;
import org.codehaus.jackson.map.ObjectMapper;
public class JacksonMapper
{
/**
*
*/
private static final ObjectMapper mapper = new ObjectMapper();
/**
*
*/
private JacksonMapper() {
}
/**
*
* @return
*/
public static ObjectMapper getInstance() {
return mapper;
}
}
pojo/bean/entity
AccountBean :
package com.json.bean;
import java.io.Serializable;
public class AccountBean implements Serializable
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 7313637781426247547L;
private int id;
private String name;
private String email;
private String address;
private Birthday birthday;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public Birthday getBirthday()
{
return birthday;
}
public void setBirthday(Birthday birthday)
{
this.birthday = birthday;
}
public AccountBean()
{
}
public AccountBean(int id, String name, String email, String address, Birthday birthday)
{
super();
this.id = id;
this.name = name;
this.email = email;
this.address = address;
this.birthday = birthday;
}
@Override
public String toString()
{
return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email;
}
}
Birthday :
package com.json.bean;
import java.io.Serializable;
public class Birthday implements Serializable
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
private String birthday;
public String getBirthday()
{
return birthday;
}
public void setBirthday(String birthday)
{
this.birthday = birthday;
}
public Birthday()
{
}
public Birthday(String birthday)
{
this.birthday=birthday;
}
@Override
public String toString()
{
return this.birthday;
}
}
测试
public class Test_me
{
private ObjectMapper objectMapper = JacksonMapper.getInstance();
private AccountBean bean = new AccountBean(23, "jack", "1290251929@qq.com", "shanghai", new Birthday("19901101"));
/* 1、JavaBean(Entity/Model)与JSON字符串的互转 */
/**
* 将bean转换成json
*/
public String writeEntityJSON()
{
try
{
String str = objectMapper.writeValueAsString(bean);
System.out.println(str);
return str;
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
/**
* 将json格式数据转换成bean
*/
public void readJson2Entity()
{
String json = "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}";
try
{
AccountBean ab= objectMapper.readValue(json, AccountBean.class);
System.out.println(ab.getName());
System.out.println(ab);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/* 2、Map集合与Json字符串互转 */
/**
* 将Map格式数据转换成Json格式
*/
public void writeMapJSON()
{
try
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("response", true);
map.put("bean1", bean);
map.put("bean2", bean);
System.out.println(objectMapper.writeValueAsString(map));
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 将json格式数据转换成Map
*/
public void readJson2Map()
{
String json =
"{\"success\":true,\"bean2\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}," + "\"bean1\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}";
System.out.println("---------------------------------readJson2Map---------------------------------------");
try
{
Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.class);
System.out.println(maps.size());
Set<String> key = maps.keySet();
Iterator<String> iter = key.iterator();
while (iter.hasNext())
{
String field = iter.next();
System.out.println(field + ":" + maps.get(field));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/* 3、List集合与Json字符串互转 */
/**
* 将List格式数据转换成Json格式
*/
public void writeListJSON()
{
try
{
List<AccountBean> list = new ArrayList<AccountBean>();
list.add(bean);
bean = new AccountBean(22, "tom", "dksf@aliyun.com", "shanghai", new Birthday("19930515"));
list.add(bean);
System.out.println("1###" + objectMapper.writeValueAsString(list));
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 将Json格式数据转换成List格式
*/
public void readJson2List_me()
{
String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"}," + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";
/*
* Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.json.bean.User
* So I had to use java.util.LinkedHashMap.
*/
try
{
List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json, List.class);
System.out.println(list.size());
for (int i = 0; i < list.size(); i++)
{
Map<String, Object> map = list.get(i);
Set<String> set = map.keySet();
for (Iterator<String> it = set.iterator(); it.hasNext();)
{
String key = it.next();
System.out.println(key + ":" + map.get(key));
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/* 4、Array与Json字符串互转 */
/**
* 将Array转换成Json格式
*/
public void Array2Json()
{
AccountBean[] arr=new AccountBean[2];
arr[0]=bean;
bean.setId(21);
arr[1]=bean;
try
{
String str= objectMapper.writeValueAsString(arr);
System.out.println(str);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 将Json格式转换成Array
*/
public void readJson2Array()
{
String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}," + "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
try
{
AccountBean[] arr = objectMapper.readValue(json, AccountBean[].class);
System.out.println(arr.length);
for (int i = 0; i < arr.length; i++)
{
AccountBean ab = arr[i];
System.out.println(ab.getAddress());
System.out.println(ab.getId());
System.out.println(ab.getName());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
关于JsonGenerator以及jackson的更多用法参见相关文档。