1. 程式人生 > >關於Jackson2.x中com.fasterxml.jackson包的用法

關於Jackson2.x中com.fasterxml.jackson包的用法

Jackson應該是目前最好的json解析工具了,之前一直用的是org.codehaus.jackson包中的工具,使用的
包是jackson-all-1.9.11.jar。

最近發現Jackson升級到2.2.3了,出於好奇,準備下載來測試一下,於是在官網下了一個jackson-core-2.2.3
的jar包,發現使用的包名都變成了com.fasterxml.jackson,而且沒有找到ObjectMapper物件,以為是整個
設計全變了,看了一下jackson-core的文件,發現有個com.fasterxml.jackson.core.JsonFactory物件,
於是用其來構造com.fasterxml.jackson.core.JsonGenerator,呼叫writeObject(pojo)方法時
報pojo不是ObjectCodec物件的錯誤……

最後經過一番檢查才發現是包沒有導全的原因,Jackson2.2.3是有jackson-core-2.2.3、jackson-annotations-2.2.3、
jackson-databind-2.2.3三個包的,新的ObjectMapper物件在jackson-databind-2.2.3包中,具體使用例項如下。
        =====檔名:JacksonCoreTest.java=====

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException
; import java.util.ArrayList; import java.util.HashMap; /** * * @author yetao */ public class JacksonCoreTest { public static void main(String[] args) throws IOException{ ArrayList<String> lists = new ArrayList(); lists.add("testlist01"); lists.add("testlist02"); HashMap
<String,String> maps = new HashMap(); maps.put("mapkey01","mapvalue01"); maps.put("mapkey02","mapvalue02"); User user1 = new User(); user1.setListinfo(lists); user1.setMapinfo(maps); user1.setNumber(100); user1.setUid(10); user1.setUname("yetao"); user1.setUpwd("666"); ObjectMapper mapper = new ObjectMapper(); // 僅輸出一行json字串 mapper.writeValue(System.out, user1); // 將字串美化成多行 System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user1)); } }
        =====檔名:User.java=====

import java.util.ArrayList;
import java.util.HashMap;

public class User {

    private int uid;
    private String uname;
    private String upwd;
    private double number;
    private ArrayList<String> listinfo;
    private HashMap<String,String> mapinfo;

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("uid:").append(uid).append("\n");
        sb.append("uname:").append(uname).append("\n");
        sb.append("upwd:").append(upwd).append("\n");
        sb.append("number:").append(number).append("\n");
        sb.append("listinfo:").append("[");
        for(String info:listinfo){
            sb.append(info).append(";");
        }
        sb.append("]\n");
        sb.append("mapinfo:").append("[");
        for(String key:mapinfo.keySet()){
            sb.append(key).append(":").append(mapinfo.get(key)).append(";");
        }
        sb.append("]\n");
        return sb.toString();
    }

    public double getNumber() {
        return number;
    }

    public void setNumber(double number) {
        this.number = number;
    }

    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;
    }

    /**
     * @return the listinfo
     */
    public ArrayList<String> getListinfo() {
        return listinfo;
    }

    /**
     * @param listinfo the listinfo to set
     */
    public void setListinfo(ArrayList<String> listinfo) {
        this.listinfo = listinfo;
    }

    /**
     * @return the mapinfo
     */
    public HashMap<String,String> getMapinfo() {
        return mapinfo;
    }

    /**
     * @param mapinfo the mapinfo to set
     */
    public void setMapinfo(HashMap<String,String> mapinfo) {
        this.mapinfo = mapinfo;
    }
}
美化成多行後的輸出如下:
{
  "uid" : 10,
  "uname" : "yetao",
  "upwd" : "666",
  "number" : 100.0,
  "listinfo" : [ "testlist01", "testlist02" ],
  "mapinfo" : {
    "mapkey02" : "mapvalue02",
    "mapkey01" : "mapvalue01"
  }
}