FastJson處理Map List 對象

 Fastjson是一個Java語言編寫的高性能功能完善的JSON庫。java

 

Fastjson是一個Java語言編寫的JSON處理器,由阿里巴巴公司開發。android

一、遵循http://json.org標準,爲其官方網站收錄的參考實現之一。git

二、功能強大,支持JDK的各類類型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。github

三、無依賴,不須要例外額外的jar,可以直接跑在JDK上。算法

四、開源,使用Apache License 2.0協議開源。http://code.alibabatech.com/wiki/display/FastJSON/Homejson

五、具備超高的性能,java世界裏沒有其餘的json庫可以和fastjson可相比了。數組

高性能數據結構

fastjson採用首創的算法,將parse的速度提高到極致,超過全部json庫,包括曾經號稱最快的jackson。而且還超越了google的二進制協議protocol buf。app

支持標準dom

Fastjson徹底支持http://json.org的標準,也是官方網站收錄的參考實現之一。

功能強大

支持各類JDK類型。包括基本類型、JavaBean、Collection、Map、Enum、泛型等。

支持循環引用

無依賴

不須要例外額外的jar,可以直接跑在JDK上。

支持範圍廣

支持JDK 五、JDK 六、Android阿里雲手機等環境。

開源

Apache License 2.0

代碼託管在github.org上,項目地址是 https://github.com/AlibabaTech/fastjson

測試充分

fastjson有超過1500個testcase,每次構建都會跑一遍,豐富的測試場景保證了功能穩定。

得到fastjson

下載

http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/

package ivyy.taobao.com.domain.fastjson;  
  
import ivyy.taobao.com.entity.Student;  
  
import java.util.ArrayList;  
import java.util.Arrays;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import com.alibaba.fastjson.JSON;  
import com.alibaba.fastjson.JSONObject;  
import com.alibaba.fastjson.TypeReference;  
  
/** 
 *@DEMO:json 
 *@Java:FastJSON.java 
 *@Date:2015-1-19上午10:28:12 
 *@Author:龍叔 
 *@Email:jilongliang@sina.com 
 *@Weibo:http://weibo.com/jilongliang 
 *@Version:1.0 
 *@Description:fastjson跟json-lib是語法很像,一句話說,全部json都差很少, 
 *你們夥也沒不要研究那麼多,懂一種本身最擅長並且熟悉能解決本身要解決的問題就OK, 
 *從fastjson反編譯過來看 你就看到pom.xml裏面的配置確定能看到json-lib,此時能 
 *證實fastjson就是運用了json-lib! 
 * 
 *-------------------------------------------- 
 *      <dependency> 
            <groupId>org.codehaus.jackson</groupId> 
            <artifactId>jackson-smile</artifactId> 
            <version>1.9.9</version> 
            <scope>test</scope> 
        </dependency> 
*-------------------------------------------- 
        <dependency> 
            <groupId>com.googlecode.json-simple</groupId> 
            <artifactId>json-simple</artifactId> 
            <version>1.1</version> 
            <scope>test</scope> 
        </dependency> 
-------------------------------------------- 
*        <dependency> 
            <groupId>net.sf.json-lib</groupId> 
            <artifactId>json-lib</artifactId> 
            <version>2.4</version> 
            <classifier>jdk15</classifier> 
            <scope>test</scope> 
        </dependency> 
-------------------------------------------- 
 */  
public class FastJSON {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) throws Exception{  
        //string2Json();  
        //string2Object();  
        //string2List();  
          
        map2json();  
        map2JSON();  
    }  
      
      
    /** 
     * 經過fastjson把字符串轉換成JSON數據 
     * TypeReference 
     */  
    public static void string2Json(){  
        StringBuffer buffer=new StringBuffer();  
        buffer.append("{");  
            buffer.append("\"age\":").append("27").append(",");  
            buffer.append("\"userName\":").append("\"龍叔\"").append(",");  
            buffer.append("\"address\":").append("\"廣東省雲浮市\"");  
        buffer.append("}");  
          
        String jsonText=buffer.toString();  
          
        JSONObject jobj=JSON.parseObject(jsonText);  
        String address=jobj.get("address").toString();  
        System.out.println(address);  
    }  
      
      
    /** 
     * 經過fastjson把字符串轉換成對象 
     * TypeReference 
     */  
    public static void string2Object(){  
        StringBuffer buffer=new StringBuffer();  
        buffer.append("{");  
            buffer.append("\"age\":").append("27").append(",");  
            buffer.append("\"userName\":").append("\"龍叔\"").append(",");  
            buffer.append("\"address\":").append("\"廣東省雲浮市\"");  
        buffer.append("}");  
          
        String jsonText=buffer.toString();  
        //方法一 把json字符串轉成Student對象  
        Student stu1 = JSON.parseObject(jsonText, new TypeReference<Student>(){});  
        //方法二 把json字符串轉成Student對象  
        Student stu2 = JSON.parseObject(jsonText,Student.class);    
          
        System.out.println(stu1.getAddress());  
        System.out.println(stu2.getAddress());  
    }  
      
    /** 
     * 經過fastjson把字符串轉換成泛型數組 
     * TypeReference 
     */  
    public static void string2List(){  
        StringBuffer buffer=new StringBuffer();  
        buffer.append("[{");  
            buffer.append("\"age\":").append("27").append(",");  
            buffer.append("\"userName\":").append("\"龍叔\"").append(",");  
            buffer.append("\"address\":").append("\"廣東省雲浮市\"");  
        buffer.append("}]");  
          
        String jsonText=buffer.toString();  
        //轉成成數組  
        Student[] stu2 = JSON.parseObject(jsonText,new TypeReference<Student[]>(){});    
        List<Student> list = Arrays.asList(stu2);  
          
        for(Student st:list){  
            System.out.println(st.getAddress());  
        }  
          
        // 轉換成ArrayList  
        ArrayList<Student> list2 = JSON.parseObject(jsonText, new TypeReference<ArrayList<Student>>(){});   
          
        for (int i = 0; i < list2.size(); i++) {  
            Student obj =(Student) list2.get(i);  
            System.out.println(obj.getAddress());  
        }  
          
    }  
    /** 
     * 經過fastjson把Map換成字符串轉 
     */  
    public static void map2json(){  
        //建立一個Map對象  
         Map<String,String> map = new HashMap<String, String>();  
         map.put("username", "周伯通");  
         map.put("address", "廣東省仙遊谷");  
         map.put("age", "198");  
         String json = JSON.toJSONString(map,true); //轉成JSON數據  
           
         Map<String,String> map1 = (Map<String,String>)JSON.parse(json);   
         //遍歷數組數據  
         for (String key : map1.keySet()) {   
            System.out.println(key+":"+map1.get(key));   
        }   
    }  
    /** 
     * 經過fastjson把Map換成字符串轉 
     */  
    public static void map2JSON() {  
        Map map = new HashMap();  
        map.put("username", "周伯通");  
        map.put("address", "廣東省仙遊谷");  
        map.put("age", "198");  
        String json = JSON.toJSONString(map);  
        Map map1 = JSON.parseObject(json);  
        for (Object obj : map.entrySet()) {  
            Map.Entry<String, String> entry = (Map.Entry<String, String>) obj;  
            System.out.println(entry.getKey() + "--->" + entry.getValue());  
        }  
    }   
}  

package ivyy.taobao.com.entity;  
  
import java.io.Serializable;  
  
/** 
 *@Author:liangjl 
 *@Date:2014-12-19 
 *@Version:1.0 
 *@Description: 
 */  
public class Student implements Serializable{  
    private Integer age;  
    private String sex;  
    private String userName;  
    private String birthday;  
    private String address;  
    private String email;  
      
    public Integer getAge() {  
        return age;  
    }  
    public void setAge(Integer age) {  
        this.age = age;  
    }  
    public String getSex() {  
        return sex;  
    }  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
    public String getUserName() {  
        return userName;  
    }  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
    public String getBirthday() {  
        return birthday;  
    }  
    public void setBirthday(String birthday) {  
        this.birthday = birthday;  
    }  
    public String getAddress() {  
        return address;  
    }  
    public void setAddress(String address) {  
        this.address = address;  
    }  
    public String getEmail() {  
        return email;  
    }  
    public void setEmail(String email) {  
        this.email = email;  
    }  
}  
相關文章
相關標籤/搜索