日期:2014-05-16 浏览次数:20458 次
package com.bbs.utils.json; 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 org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.JsonNodeFactory; public class JacksonTest { private static JsonGenerator jsonGenerator = null; private static ObjectMapper objectMapper = null; private static User user = null; public static void writeEntity2Json() throws IOException { System.out.println("使用JsonGenerator转化实体为json串-------------"); // writeObject可以转换java对象,eg:JavaBean/Map/List/Array等 jsonGenerator.writeObject(user); System.out.println(); System.out.println("使用ObjectMapper-----------"); // writeValue具有和writeObject相同的功能 objectMapper.writeValue(System.out, user); } public static void writeList2Json() throws IOException { List<User> userList = new ArrayList<User>(); userList.add(user); User u = new User(); u.setUid(10); u.setUname("archie"); u.setUpwd("123"); userList.add(u); objectMapper.writeValue(System.out, userList); } public static void writeMap2Json() { try { Map<String, Object> map = new HashMap<String, Object>(); User u = new User(); u.setUid(10); u.setUname("archie"); u.setUpwd("123"); map.put("uid", u.getUid()); map.put("uname", u.getUname()); map.put("upwd", u.getUpwd()); System.out.println("jsonGenerator"); jsonGenerator.writeObject(map); objectMapper.writeValue(System.out, map); } catch (IOException e) { e.printStackTrace(); } } public static void writeOthersJSON() { try { String[] arr = { "a", "b", "c" }; System.out.println("jsonGenerator"); String str = "hello world jackson!"; // byte jsonGenerator.writeBinary(str.getBytes()); // boolean jsonGenerator.writeBoolean(true); // null jsonGenerator.writeNull(); // float jsonGenerator.writeNumber(2.2f); // char jsonGenerator.writeRaw("c"); // String jsonGenerator.writeRaw(str, 5, 10); // String jsonGenerator.writeRawValue(str, 5, 5); // String jsonGenerator.writeString(str); jsonGenerator.writeTree(JsonNodeFactory.instance.POJONode(str)); System.out.println(); // Object jsonGenerator.writeStartObject();// { jsonGenerator.writeObjectFieldStart("user");// user: jsonGenerator.writeStringField("name", "jackson");// name:jackson jsonGenerator.writeBooleanField("sex", true);// sex:true jsonGenerator.writeNumberField("age", 22);// age:22 jsonGenerator.writeEndObject(); jsonGenerator.writeArrayFieldStart("infos");// infos:[ jsonGenerator.writeNumber(22);// 22 jsonGenerator.writeString("this is array");// this is array jsonGenerator.writeEndArray();// ] jsonGenerator.writeEndObject();// } User u = new User(); user.setUid(5); user.setUname("tom"); user.setUpwd("123"); user.setNumber(3.44); // complex Object jsonGenerator.writeStartObject();// { jsonGenerator.writeObjectField("uid", u);// user:{bean} jsonGenerator.writeObjectField("infos", arr);// infos:[array] jsonGenerator.writeEndObject();// } } catch (Exception e) { e.printStackTrace(); } } /** * JSON字符串转换为对象 */ public static void readJson2Entity() { String json = "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}"; try { User acc = objectMapper.readValue(json, User.class); System.out.println(acc.getUid()); System.out.println(acc); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.p