FastJson的簡單實用

1、FastJson的理解java

在工做中,常常客服端須要和服務端進行通訊,目前不少項目都採用JSON的方式進行數據傳輸,簡單的參數能夠經過手動拼接JSON字符串,但若是請求的參數過多,採用手動拼接JSON字符串,出錯率就很是大了。而且工做效率也特別低。git

經常使用一些開源的JSON框架,好比Google提供的Gson,Jackson,FastJson等框架。
FastJson不依賴於第三方包, 直接能夠運行在Java JDK1.5之上,FastJson徹底支持
http://json.org的標準,支持各類JDK類型,包括基本類型、JavaBean、Collection、Map、Enum、泛型等
還支持循環引用。
FastJson項目是開源的:Fastjson代碼託管在github.org上,項目地址是
https://github.com/AlibabaTech/fastjsongithub

一個JSON庫涉及的最基本功能就是序列化和反序列化。Fastjson支持java bean的直接序列化。使用com.alibaba.fastjson.JSON這個類進行序列化和反序列化。json

FastJson是阿里巴巴工程師設計的框架

2、FastJson的基本使用spa

package cn.monster.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * FastJson對json數據的處理
 * @author monster
 * @date 2015-08-10
 * 
 *FastJson的經常使用用法
 *public static final Object parse(String text); // 把JSON文本parse爲JSONObject或者JSONArray 
 *public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
 *------>>>public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse爲JavaBean 
 *public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
 *public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 
 *---->>>>public static final String toJSONString(Object object); // 將JavaBean序列化爲JSON文本  
 *---->>>>public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化爲帶格式的JSON文本 
 *---->>>>public static final Object toJSON(Object javaObject); //將JavaBean轉換爲JSONObject或者JSONArray。
 */
public class FastJson {
    public static void main(String[] args) {
            User user=new User();
            user.setName("monster");
            user.setAge(21);
            String json=JSON.toJSONString(user); //將數據解析成json數據
            System.out.println("JSON :" +json);
          
            User Asyuser=JSON.parseObject(json, User.class);
            System.out.println("User  "+Asyuser.getName());
        
        //數據--->json數據
        HashMap<String, Object> map=new HashMap<String, Object>();
        map.put("username"," monster");
        map.put("age", 24);
        map.put("sex","boy");
        
        HashMap<String,Object> temp=new HashMap<String,Object>();
        temp.put("name", "YY");
        temp.put("age", 21);
        temp.put("sex","girl");
        
        map.put("girlInfo", temp);  //--->把temp添加到map中,由於map這個對象的值的數據類型爲Object
        
        List<String> list=new ArrayList<String>();
        list.add("登山");
        list.add("騎車");
        list.add("旅遊");
        map.put("hobby", list);
         
        //JSON 序列化,默認序列化出的JSON字符串中鍵值對是使用雙引號,若是須要單引號的JSON字符串
        String jsonString = JSON.toJSONString(map);   //雙引號
        //String jsonString = JSON.toJSONString(map,   SerializerFeature.UseSingleQuotes); //-->單引號
        System.out.println("JSON=" + jsonString);  
    
        //輸出結果以下
        /**
          * JSON={
                "sex": "boy",
                "username": " monster",
                "age": 24,
                "hobby": [
                    "登山",
                    "騎車",
                    "旅遊"
                ],
                "girlInfo": {
                    "sex": "girl",
                    "age": 21,
                    "name": "YY"
                }
            }
         */
        
        
        /**
         * json數據解析
          */
        String newJsonString="{\"name\":\"chenggang\",\"age\":24}"; 
         //反序列化 
         User userInfo=JSON.parseObject(newJsonString,User.class); 
         System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());  
         
         /**
           * 日期格式化
             */
          Date date=new Date();   
          //輸出毫秒值 
          System.out.println(JSON.toJSONString(date)); 
          //默認格式爲yyyy-MM-dd HH:mm:ss   
          System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat)); 
          //根據自定義格式輸出日期  
          System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
          
          /**
           *   輸出結果
             *   1439164604282
           *    "2015-08-10 07:56:44"
           *    "2015-08-10"
           */
    }

}
相關文章
相關標籤/搜索