同时Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。功能非常的强悍!
大家也知道,json 在如今互联网时代应用的非常广,因为大家如此的关注,所以对json的解析性能要求也是非常高的。
一、 准备工作
1、 下载依赖库jar包
Jackson的jar all下载地址:http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar
然后在工程中导入这个jar包即可开始工作
官方示例:http://wiki.fasterxml.com/JacksonInFiveMinutes
因为下面的程序是用junit测试用例运行的,所以还得添加junit的jar包。版本是junit-4.2.8
如果你需要转换xml,那么还需要stax2-api.jar
2、 测试类基本代码如下
- /*
- * @project java
- * @package
- * @file Jackson.java
- * @version 1.0
- * @author 廖益平
- * @time 2011-11-8 上午02:59:37
- */
- public class Jackson {
- /*
- *
- * Class Descripton goes here.
- *
- * @class Jackson
- * @version 1.0
- * @author 廖益平
- * @time 2011-11-8 上午02:59:37
- */
- public static JsonGenerator jsonGenerator = null;
- private static ObjectMapper mapper = new ObjectMapper();
- public static void main(String[] args) {
- Student student = new Student();
- student.setIsstudent(true);
- student.setUid(1000);
- student.setUname("xiao liao");
- student.setUpwd("123");
- student.setNumber(12);
- Map<String, Student> stuMap = new HashMap<String, Student>();
- stuMap.put("1", student);
- stuMap.put("2", student);
- List<Object> stuList = new ArrayList<Object>();
- List<Student> stuList1 = new ArrayList<Student>();
- stuList1.add(student);
- student= new Student();
- student.setIsstudent(false);
- student.setUid(200);
- student.setUname("xiao mi");
- stuList1.add(student);
- stuList.add(student);
- stuList.add("xiao xin");
- stuList.add("xiao er");
- stuList.add(stuMap);
- //readJson2List();
- try {
- //readJson2Array();
- //writeArray2Json(array);
- //writeJson2List();
- //writeEntity2Json(student);
- writeJson2Entity();
- //writeMap2Json(stuMap);
- //writeList2Json(stuList1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- *
- * <code>writeEntity2Json</code>
- * @description: TODO(实体类转换成json)
- * @param object
- * @throws IOException
- * @since 2011-11-8 廖益平
- */
- public static void writeEntity2Json(Object object) throws IOException {
- mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
- mapper.writeValue( System.out,object );
- }
- /**
- *
- * <code>writeArray2Json</code>
- * @description: TODO(数组转换成json数组)
- * @param object
- * @throws IOException
- * @since 2011-11-8 廖益平
- */
- public static void writeArray2Json(Object object) throws IOException {
- // writeValue具有和writeObject相同的功能
- mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
- mapper.writeValue(System.out,object );
- }
- /**
- *
- * <code>writeMap2Json</code>
- * @description: TODO(map对象转换成json对象)
- * @param object
- * @throws IOException
- * @since 2011-11-8 廖益平
- */
- public static void writeMap2Json(Object object) throws IOException {
- System.out.println("使用ObjectMapper-----------");
- // writeValue具有和writeObject相同的功能
- System.out.println("==>"+mapper.writeValueAsString(object));
- mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );
- mapper.writeValue( System.out , object );
- }
- /**
- *
- * <code>writeList2Json</code>
- * @description: TODO(list转换成json)
- * @param object
- * @throws IOException
- * @since 2011-11-8 廖益平
- */
- public static void writeList2Json(Object object) throws IOException {
- System.out.println("==>"+mapper.writeValueAsString(object));
- mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );
- mapper.writeValue( System.out , object );
- }
- /**
- *
- * <code>writeJson2Entity</code>
- * @description: TODO(json转换成实体)
- * @throws IOException
- * @since 2011-11-8 廖益平
- */
- public static void writeJson2Entity() throws IOException {
- System.out.println("json串转换成entity-------------");
- // File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");
- // FileInputStream inputStream = new FileInputStream(file);
- // Student student = mapper.readValue(inputStream,Student.class);
- // System.out.println(student.toString());
- //漂亮输出
- //mapper.defaultPrettyPrintingWriter().writeValueAsString(value);
- String json = "{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true}";
- Student student1 = mapper.readValue(json,Student.class);
- System.out.println("json2:"+student1.toString());
- }
- /**
- *
- * <code>writeJson2List</code>
- * @description: TODO(json专程list对象)
- * @throws IOException
- * @since 2011-11-8 廖益平
- */
- public static void writeJson2List() throws IOException {
- System.out.println("json串转换成entity-------------");
- File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");
- FileInputStream inputStream = new FileInputStream(file);
- Student student = mapper.readValue(inputStream,Student.class);
- System.out.println(student.toString());
- String json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]";
- List<LinkedHashMap<String, Object>> s= mapper.readValue(json,List.class);
- for (int i = 0; i < s.size(); i++) {
- LinkedHashMap<String, Object> link = s.get(i);
- Set<String> key = link.keySet();
- for (Iterator iterator = key.iterator(); iterator.hasNext();) {
- String string = (String) iterator.next();
- System.out.println(string+"==>"+link.get(string));
- }
- System.out.println("json:"+i+""+s.get(i).toString());
- }
- }
- /**
- * JSON转换为List对象
- */
- public static void readJson2List() {
- String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"
- + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";
- try {
- List<LinkedHashMap<String, Object>> list = mapper.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转换为List对象
- */
- public static void readJson2Array() {
- String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"
- + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";
- try {
- Student[] students = mapper.readValue(json, Student[].class);
- for (Student student : students) {
- System.out.println(">"+student.toString());
- }
- } catch (JsonParseException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
打印结果 :
串转换成entity-------------
json2:uid=1000,name=xiao liao,upwd=123,number=12.0,isStudent=true
writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}
readJson2Array------------------
>uid=1,name=www,upwd=456,number=234.0,isStudent=false
>uid=5,name=tom,upwd=123,number=3.44,isStudent=false
writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}
大家逐个自己试试吧 ,上面也是我的测试代码
- 实体类:
- /*
- * @project java
- * @package
- * @file Student.java
- * @version 1.0
- * @author 廖益平
- * @time 2011-11-8 上午03:01:08
- */
- public class Student {
- /*
- *
- * Class Descripton goes here.
- *
- * @class Student
- * @version 1.0
- * @author 廖益平
- * @time 2011-11-8 上午03:01:08
- */
- private int uid;
- private String uname;
- private String upwd;
- private double number;
- private boolean isstudent;
- public int getUid() {
- return uid;
- }
- public void setUid(int uid) {
- this.uid = uid;
- }
- public String getUname() {
- return uname;
- }
- public void setUname(String uname) {
- this.uname = uname;
- }
- public String getUpwd() {
- return upwd;
- }
- public void setUpwd(String upwd) {
- this.upwd = upwd;
- }
- public double getNumber() {
- return number;
- }
- public void setNumber(double number) {
- this.number = number;
- }
- public boolean isIsstudent() {
- return isstudent;
- }
- public void setIsstudent(boolean isstudent) {
- this.isstudent = isstudent;
- }
- @Override
- public String toString() {
- return "uid="+uid+",name="+uname+",upwd="+upwd+",number="+number+",isStudent="+isstudent;
- }
- }