日期:2014-05-16 浏览次数:20429 次
package com.demo.test; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.JsonNodeFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.demo.entity.Account; public class JacksonTest { private JsonGenerator jsonGenerator = null; private ObjectMapper objectMapper = null; private Account bean = null; @Before public void init() { bean = new Account(); bean.setAddress("CHINA-BJ"); bean.setEmail("11@qq.com"); bean.setId(1); bean.setName("haha"); objectMapper = new ObjectMapper(); try { jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8); } catch (IOException e) { e.printStackTrace(); } } @After public void destory() { try { if (jsonGenerator != null) { jsonGenerator.flush(); } if (!jsonGenerator.isClosed()) { jsonGenerator.close(); } jsonGenerator = null; objectMapper = null; } catch (Exception e) { e.printStackTrace(); } } @Test 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 (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void writeMapJson() { try{ Map<String, Object> map = new HashMap<String, Object>(); map.put("name", bean.getName()); map.put("account", bean); bean = new Account(); bean.setAddress("CHINA-GZ"); bean.setEmail("22@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 (Exception e) { e.printStackTrace(); } } @Test public void writeListJson() { try{ List<Account> accounts = new ArrayList<Account>(); accounts.add(bean); bean = new Account(); bean.setId(2); bean.setName("lala"); bean.setAddress("CHINA-GZ"); bean.setEmail("22@qq.com"); accounts.add(bean); System.out.println("jsonGenerator"); jsonGenerator.writeObject(accounts); System.out.println(); System.out.println("objectMapper"); System.out.println("1###" + objectMapper.writeValueAsString(accounts)); System.out.println("2###"); objectMapper.writeValue(System.out, accounts); } catch (Exception e) { e.printStackTrace(); } } @Test public void writeOthersJson() { try{ String[] arr = {"abc", "def", "jhi"}; System.out.println("jsonGenerator"); String str = "Hello Jackson!"; //byte jsonGenerator.writeBinary(str.getBytes()); //boolean jsonGenerator.writeBoolean(true); //null jsonGenerator.writeNull(); //float jsonGenerator.writeNumber(2.5f); //char jsonGenerator.writeRaw("c"); //String jsonGenerator.writeRaw(str, 1, 5); //String jsonGenerator.writeRawValue(str, 5, 5); //String jsonGenerator.writeString(str); jsonGenerator.writeTree(JsonNodeFactory.instance.POJONode(str)); System.out.println(); //Objcet {"user":{"name":"lala","sex":true,"age":26},"infos":[22,"Hello Jackson"]} jsonGenerator.writeStartObject(); //{ jsonGenerator.writeObjectFieldStart("user"); //user:{ jsonGenerator.writeStringF