fastjson用法

package fastJson.test;  
  
import com.alibaba.fastjson.JSON;  
import com.alibaba.fastjson.JSONObject;  
import com.alibaba.fastjson.serializer.SerializerFeature;  
  
public class FastJsonTest  
{  
  
    /** 
     * 序列化 
     */  
    public void toJsonString()  
    {  
        User user = new User("testFastJson001", "maks", 105);  
        String text = JSON.toJSONString(user);  
        System.out.println("toJsonString()方法:text=" + text);  
        // 輸出結果:text={"age":105,"id":"testFastJson001","name":"maks"}  
    }  
  
    /** 
     * 反序列化爲json對象 
     */  
    public void parseJsonObject()  
    {  
        String text = "{\"age\":105,\"id\":\"testFastJson001\",\"name\":\"maks\"}";  
        JSONObject json = JSON.parseObject(text);  
        System.out.println("parseJsonObject()方法:json==" + json);  
        // 輸出結果:json=={"age":105,"id":"testFastJson001","name":"maks"}  
    }  
  
    /** 
     * 反序列化爲javaBean對象 
     */  
    public void parseBeanObject()  
    {  
        String text = "{\"age\":105,\"id\":\"testFastJson001\",\"name\":\"maks\"}";  
        User user = (User) JSON.parseObject(text, User.class);  
        System.out.println("parseBeanObject()方法:user==" + user.getId() + "," + user.getName() + "," + user.getAge());  
        // 輸出結果:user==testFastJson001,maks,105  
    }  
  
    /** 
     * 將javaBean轉化爲json對象 
     */  
    public void bean2Json()  
    {  
        User user = new User("testFastJson001", "maks", 105);  
        JSONObject jsonObj = (JSONObject) JSON.toJSON(user);  
        System.out.println("bean2Json()方法:jsonObj==" + jsonObj);  
        // 輸出結果:jsonObj=={"age":105,"id":"testFastJson001","name":"maks"}  
    }  
  
    /** 
     * 全序列化 直接把java bean序列化爲json文本以後,可以按照原來的類型反序列化回來。支持全序列化,須要打開SerializerFeature.WriteClassName特性 
     */  
    public void parseJSONAndBeanEachother()  
    {  
        User user = new User("testFastJson001", "maks", 105);  
        SerializerFeature[] featureArr = { SerializerFeature.WriteClassName };  
        String text = JSON.toJSONString(user, featureArr);  
        System.out.println("parseJSONAndBeanEachother()方法:text==" + text);  
        // 輸出結果:text=={"@type":"fastJson.test.User","age":105,"id":"testFastJson001","name":"maks"}  
        User userObj = (User) JSON.parse(text);  
        System.out.println("parseJSONAndBeanEachother()方法:userObj==" + userObj.getId() + "," + userObj.getName() + "," + userObj.getAge());  
        // 輸出結果:userObj==testFastJson001,maks,105  
    }  
  
    public static void main(String[] args)  
    {  
        FastJsonTest test = new FastJsonTest();  
        // 序列化  
        test.toJsonString();  
        // 反序列化爲json對象  
        test.parseJsonObject();  
        // 反序列化爲javaBean對象  
        test.parseBeanObject();  
        // 將javaBean轉化爲json對象  
        test.bean2Json();  
        // 全序列化  
        test.parseJSONAndBeanEachother();  
    }  
}  

  輸出結果:java

toJsonString()方法:text={"age":105,"id":"testFastJson001","name":"maks"}  
parseJsonObject()方法:json=={"age":105,"id":"testFastJson001","name":"maks"}  
parseBeanObject()方法:user==testFastJson001,maks,105  
bean2Json()方法:jsonObj=={"age":105,"id":"testFastJson001","name":"maks"}  
parseJSONAndBeanEachother()方法:text=={"@type":"fastJson.test.User","age":105,"id":"testFastJson001","name":"maks"}  
parseJSONAndBeanEachother()方法:userObj==testFastJson001,maks,105  
相關文章
相關標籤/搜索