標籤:objectmapp json 對象轉json json轉list json轉map
- 概述
- JacksonTest
- AccountBean
- Birthday
概述
原文連結:http://blog.csdn.net/u011506468/article/details/47342667
最近用到了ObjectMapper,做了些實驗。主要有以下一些轉換方式:
- JavaBean(Entity/Model)轉換成JSON
- 將Map集合轉換成Json字串
- 將List集合轉換成json
- 將json字串轉換成JavaBean對象
- 將json字串轉換成List集合
- 將json字串轉換成List集合
- Json字串轉換成Array數組
- Json字串轉換成Map集合
代碼如下,全部copy過去即可
JacksonTest
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("[email protected]"); 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("[email protected]"); 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(); } }}
AccountBean
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; }}
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操作對象和集合的一些相互轉換用法