标签:objectmapp json 对象转json json转list json转map
原文链接:http://blog.csdn.net/u011506468/article/details/47342667
最近用到了ObjectMapper,做了些实验。主要有以下一些转换方式:
代码如下,全部copy过去即可
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTest {
private JsonGenerator jsonGenerator = null;
private ObjectMapper objectMapper = null;
private AccountBean bean = null;
public static void main (String[] args) {
JacksonTest test = new JacksonTest();
test.init();
/* JavaBean(Entity/Model)转换成JSON */
//test.writeEntityJSON();
/* 将Map集合转换成Json字符串 */
//test.writeMapJSON();
/* 将List集合转换成json */
//test.writeListJSON();
/* 将json字符串转换成JavaBean对象 */
//test.readJson2Entity();
/* 将json字符串转换成List<Map>集合 */
//test.readJson2List();
/*将json字符串转换成List<T>集合*/
//test.readJson2ListT();
/* Json字符串转换成Array数组 */
//test.readJson2Array();
/* Json字符串转换成Map集合 */
test.readJson2Map();
test.destory();
}
/* Json字符串转换成Map集合 */
public void readJson2Map() {
String json = "{\"success\":true,\"A\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+
"\"B\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}";
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 (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException 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++) {
System.out.println(arr[i]);
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*将json字符串转换成List<T>集合*/
public void readJson2ListT() {
String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+
"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
try {
List<AccountBean> list = objectMapper.readValue(json, new TypeReference<List<AccountBean>>(){} );
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/* 将json字符串转换成List<Map>集合 */
public void readJson2List() {
String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+
"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
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 (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/* 将json字符串转换成JavaBean对象 */
public void readJson2Entity() {
String json = "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}";
try {
AccountBean acc = objectMapper.readValue(json, AccountBean.class);
System.out.println(acc.getName());
System.out.println(acc);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/* 将List集合转换成json */
public void writeListJSON() {
try {
List<AccountBean> list = new ArrayList<AccountBean>();
list.add(bean);
bean = new AccountBean();
bean.setId(2);
bean.setAddress("address2");
bean.setEmail("email2");
bean.setName("haha2");
list.add(bean);
System.out.println("jsonGenerator");
//list转换成JSON字符串
jsonGenerator.writeObject(list);
System.out.println();
System.out.println("ObjectMapper");
//用objectMapper直接返回list转换成的JSON字符串
System.out.println("1###" + objectMapper.writeValueAsString(list));
System.out.print("2###");
//objectMapper list转换成JSON字符串
objectMapper.writeValue(System.out, list);
} catch (IOException e) {
e.printStackTrace();
}
}
/* 将Map集合转换成Json字符串 */
public void writeMapJSON() {
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", bean.getName());
map.put("account", bean);
bean = new AccountBean();
bean.setAddress("china-Beijin");
bean.setEmail("hoojo@qq.com");
map.put("account2", bean);
System.out.println("jsonGenerator");
jsonGenerator.writeObject(map);
System.out.println("");
System.out.println("objectMapper");
objectMapper.writeValue(System.out, map);
} catch (IOException e) {
e.printStackTrace();
}
}
/* JavaBean(Entity/Model)转换成JSON */
public void writeEntityJSON() {
try {
System.out.println("jsonGenerator");
//writeObject可以转换java对象,eg:JavaBean/Map/List/Array等
jsonGenerator.writeObject(bean);
System.out.println();
System.out.println("ObjectMapper");
//writeValue具有和writeObject相同的功能
objectMapper.writeValue(System.out, bean);
} catch (IOException e) {
e.printStackTrace();
}
}
/* 初始化 */
public void init() {
bean = new AccountBean();
bean.setAddress("china-Guangzhou");
bean.setEmail("hoojo_@126.com");
bean.setId(1);
bean.setName("hoojo");
objectMapper = new ObjectMapper();
try {
jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
} catch (IOException e) {
e.printStackTrace();
}
}
/* 释放资源 */
public void destory() {
try {
if (jsonGenerator != null) {
jsonGenerator.flush();
}
if (!jsonGenerator.isClosed()) {
jsonGenerator.close();
}
jsonGenerator = null;
objectMapper = null;
bean = null;
System.gc();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class AccountBean {
private int id;
private String name;
private String email;
private String address;
private Birthday birthday;
//getter、setter
@Override
public String toString() {
return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email;
}
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 class Birthday {
private String birthday;
public Birthday(String birthday) {
super();
this.birthday = birthday;
}
//getter、setter
public Birthday() {}
@Override
public String toString() {
return this.birthday;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
com.fasterxml.jackson.databind.ObjectMapper操作对象和集合的一些相互转换用法
标签:objectmapp json 对象转json json转list json转map
原文地址:http://blog.csdn.net/u011506468/article/details/47342667