用jackson包實現json、對象、Map之間的轉換

主要用於測試學習用jackson包實現json、對象、Map之間的轉換。java

一、準備測試用的Java類json

(1)Link類數組

package test;  
  
/**  
 * Description: 聯繫方式,被user使用 
 */  
public class Link  
{  
    private String phone; //移動電話  
    private String address; //地址  
    private String qq; //QQ  
      
    public String getPhone()  
    {  
        return phone;  
    }  
    public void setPhone(String phone)  
    {  
        this.phone = phone;  
    }  
    public String getAddress()  
    {  
        return address;  
    }  
    public void setAddress(String address)  
    {  
        this.address = address;  
    }  
    public String getQq()  
    {  
        return qq;  
    }  
    public void setQq(String qq)  
    {  
        this.qq = qq;  
    }  
}

(2)User類app

package test;  
  
import java.util.Date;  
import java.util.List;  
import java.util.Map;  
import java.util.Set;  
  
/**  
 * Description: 用戶,測試模型類 
 */  
public class User  
{  
    private String id; //標識  
    private String name;    //姓名  
    private int age;    //年齡  
    private double pay; //工資   
    private boolean valid;  //是否有效  
    private char one; //一個字符  
    private Date birthday;  //生日  
      
    private Link link; //聯繫方式,自定義  
      
    private Map map; //測試用  
    private List list; //測試用  
    private Set set; //測試用  
    public String getId()  
    {  
        return id;  
    }  
    public void setId(String id)  
    {  
        this.id = id;  
    }  
    public String getName()  
    {  
        return name;  
    }  
    public void setName(String name)  
    {  
        this.name = name;  
    }  
    public int getAge()  
    {  
        return age;  
    }  
    public void setAge(int age)  
    {  
        this.age = age;  
    }  
    public double getPay()  
    {  
        return pay;  
    }  
    public void setPay(double pay)  
    {  
        this.pay = pay;  
    }  
    public boolean isValid()  
    {  
        return valid;  
    }  
    public void setValid(boolean valid)  
    {  
        this.valid = valid;  
    }  
    public char getOne()  
    {  
        return one;  
    }  
    public void setOne(char one)  
    {  
        this.one = one;  
    }  
    public Date getBirthday()  
    {  
        return birthday;  
    }  
    public void setBirthday(Date birthday)  
    {  
        this.birthday = birthday;  
    }  
    public Link getLink()  
    {  
        return link;  
    }  
    public void setLink(Link link)  
    {  
        this.link = link;  
    }  
    public Map getMap()  
    {  
        return map;  
    }  
    public void setMap(Map map)  
    {  
        this.map = map;  
    }  
    public List getList()  
    {  
        return list;  
    }  
    public void setList(List list)  
    {  
        this.list = list;  
    }  
    public Set getSet()  
    {  
        return set;  
    }  
    public void setSet(Set set)  
    {  
        this.set = set;  
    }  
}

二、測試類,沒用Junit,用Main()方法輸出學習

package test;  
  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.HashMap;  
import java.util.HashSet;  
import java.util.List;  
import java.util.Map;  
import java.util.Set;  
import org.codehaus.jackson.JsonGenerationException;  
import org.codehaus.jackson.map.JsonMappingException;  
import org.codehaus.jackson.map.ObjectMapper;  
  
/**  
 * Description: 主要用於測試學習用jackson包實現json、對象、Map之間的轉換 
 *  
 * Date: 2016年6月6日 
 * Time: 下午3:23:23 
 */  
public class JsonTest  
{  
  
    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException  
    {  
        User user=new User();  
        user.setId("01");  
        user.setName("張三");  
        user.setAge(33);  
        user.setPay(6666.88);  
        user.setValid(true);  
        user.setOne('E');  
        user.setBirthday(new Date(20l*366*24*3600*1000)); //1990年  
          
        Link link = new Link();  
        link.setAddress("河南省濟源市");  
        link.setPhone("13899995555");  
        link.setQq("123456");  
        user.setLink(link);  
          
        Map map=new HashMap();  
        map.put("aa", "this is aa");  
        map.put("bb", "this is bb");  
        map.put("cc", "this is cc");  
        user.setMap(map);  
          
        List list=new ArrayList(){};  
        list.add("普洱");  
        list.add("大紅袍");  
        user.setList(list);  
          
        Set set=new HashSet();  
        set.add("籃球");  
        set.add("足球");  
        set.add("乒乓球");  
        user.setSet(set);  
          
        ObjectMapper mapper = new ObjectMapper(); //轉換器  
          
        //測試01:對象--json  
        String json=mapper.writeValueAsString(user); //將對象轉換成json  
        System.out.println(json);  
        /* 結果以下: 
        {"id":"01","name":"張三","age":33,"pay":6666.88,"valid":true,"one":"E","birthday":1465185448998, 
        "link":{"phone":"13899995555","address":"河南省濟源市","qq":"123456"}, 
        "map":{"aa":"this is aa","bb":"this is bb","cc":"this is cc"}, 
        "list":["普洱","大紅袍"], 
        "set":["乒乓球","足球","籃球"]} 
         
        注意點:(1) 日期--長整型 (2)List、Set均轉成數組 
         */  
          
        //測試02:json--map  
        Map m = mapper.readValue(json, Map.class); //json轉換成map  
          
        System.out.println("pay:"+m.get("pay").getClass().getName()); //java.lang.Double  
        System.out.println("valid:"+m.get("valid").getClass().getName()); //java.lang.Boolean  
        System.out.println("birthday:"+m.get("birthday").getClass().getName()); //java.lang.Long  
        System.out.println("link:"+m.get("link").getClass().getName()); //java.util.LinkedHashMap  
        System.out.println("map:"+m.get("map").getClass().getName()); //java.util.LinkedHashMap  
        System.out.println("list:"+m.get("list").getClass().getName()); //java.util.ArrayList  
        System.out.println("set:"+m.get("set").getClass().getName()); //java.util.ArrayList  
        /*  結果以下: 
            pay:java.lang.Double 
            valid:java.lang.Boolean 
            birthday:java.lang.Long 
            link:java.util.LinkedHashMap 
            map:java.util.LinkedHashMap 
            list:java.util.ArrayList 
            set:java.util.ArrayList 
             
            注意點:(1) 日期--長整型  (2)map、子對象均轉換成了LinkedHashMap (3)List、Set均轉成ArrayList 
         */  
          
        //測試03:map--json  
        json=mapper.writeValueAsString(m); //map轉json  
        System.out.println(json); //與以前格式徹底相同,說明通過map轉換後,信息沒有丟失  
          
        //測試04:json--對象  
        User u=mapper.readValue(json, User.class); //json轉java對象。經測,轉成對象後,一切恢復正常  
        System.out.println("pay:"+u.getPay());  
        System.out.println("valid:"+u.isValid());  
        System.out.println("birthday:"+u.getBirthday());  
        System.out.println("link:"+u.getLink());  
        System.out.println("map:"+u.getMap());  
        System.out.println("list:"+u.getList());  
        System.out.println("set:"+u.getSet());  
          
        //測試05:其餘轉換  
        byte[] data=mapper.writeValueAsBytes(u); //對象轉成二進制數組  
          
          
    }  
  
}

三、輸出結果測試

{"id":"01","name":"張三","age":33,"pay":6666.88,"valid":true,"one":"E","birthday":632448000000,"link":{"phone":"13899995555","address":"河南省濟源市","qq":"123456"},"map":{"aa":"this is aa","bb":"this is bb","cc":"this is cc"},"list":["普洱","大紅袍"],"set":["乒乓球","足球","籃球"]}  
pay:java.lang.Double  
valid:java.lang.Boolean  
birthday:java.lang.Long  
link:java.util.LinkedHashMap  
map:java.util.LinkedHashMap  
list:java.util.ArrayList  
set:java.util.ArrayList  
{"id":"01","name":"張三","age":33,"pay":6666.88,"valid":true,"one":"E","birthday":632448000000,"link":{"phone":"13899995555","address":"河南省濟源市","qq":"123456"},"map":{"aa":"this is aa","bb":"this is bb","cc":"this is cc"},"list":["普洱","大紅袍"],"set":["乒乓球","足球","籃球"]}  
pay:6666.88  
valid:true  
birthday:Tue Jan 16 08:00:00 CST 1990  
link:test.Link@350385f1  
map:{aa=this is aa, bb=this is bb, cc=this is cc}  
list:[普洱, 大紅袍]  
set:[足球, 乒乓球, 籃球]  this

相關文章
相關標籤/搜索