FastJson是什麼?java
從網上查到---json
官網地址:http://code.alibabatech.com/wiki/display/FastJSON/Overview(已關閉)工具
FastJSOn是阿里巴巴開源的JSON處理工具,包括「序列化」和「反序列化」兩部分,它具有以下特徵:性能
既然速度快,固然要體驗一下。更況且是國產貨,固然要支持了。測試一下,之後在項目裏使用...測試
1 package test; 2 import java.io.Serializable; 3 /** 4 * <一句話功能簡述> 5 * @author aliger Email:liqimo#gmail.com 6 * @version [V1.00, 2014-8-16] 7 * @see [相關類/方法] 8 * @since V1.00 9 */ 10 public class UserInfo implements Serializable{ 11 private String name; 12 private int age; 13 public void setName(String name){ 14 this.name=name; 15 } 16 public String getName(){ 17 return name; 18 } 19 public void setAge(int age){ 20 this.age=age; 21 } 22 public int getAge(){ 23 return age; 24 } 25 }
Main:this
1 package test; 2 import java.util.ArrayList; 3 import java.util.Date; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import com.alibaba.fastjson.JSON; 8 import com.alibaba.fastjson.TypeReference; 9 import com.alibaba.fastjson.serializer.SerializerFeature; 10 /** 11 * <一句話功能簡述> 12 * 13 * @author aliger Email:liqimo#gmail.com 14 * @version [V1.00, 2014-8-16] 15 * @see [相關類/方法] 16 * @since V1.00 17 */ 18 public class TestOne 19 { 20 public static FastJsonUtils ff; 21 22 //public static void main(String[] args){ 23 public static void test1(){ 24 UserInfo info=new UserInfo(); 25 info.setName("zhangsan"); 26 info.setAge(24); 27 //將對象轉換爲JSON字符串 28 //String str_json=JSON.toJSONString(info); 29 String str_json=ff.getJsonString(info); 30 System.out.println("JSON="+str_json); 31 } 32 public static void test2() { 33 34 HashMap<String, Object> map = new HashMap<String, Object>(); 35 map.put("username", "zhangsan"); 36 map.put("age", 24); 37 map.put("sex", "男"); 38 //map集合 39 HashMap<String, Object> temp = new HashMap<String, Object>(); 40 temp.put("name", "xiaohong"); 41 temp.put("age", "23"); 42 map.put("girlInfo", temp); 43 //list集合 44 List<String> list = new ArrayList<String>(); 45 list.add("登山"); 46 list.add("騎車"); 47 list.add("旅遊"); 48 map.put("hobby", list); 49 /*JSON 序列化,默認序列化出的JSON字符串中鍵值對是使用雙引號,若是須要單引號的JSON字符串, [eg:String jsonString = JSON.toJSONString(map, SerializerFeature.UseSingleQuotes);] 50 *fastjson序列化時能夠選擇的SerializerFeature有十幾個屬性,你能夠按照本身的須要去選擇使用。 51 */ 52 String jsonString = JSON.toJSONString(map); 53 System.out.println("JSON===" + jsonString); 54 } 55 public static void test3(){ 56 String json="{\"name\":\"chenggang\",\"age\":24}"; 57 //反序列化 58 UserInfo userInfo=JSON.parseObject(json,UserInfo.class); 59 System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge()); 60 } 61 /**泛型的反序列化*/ 62 public static void test4(){ 63 String json="{\"user\":{\"name\":\"zhangsan\",\"age\":25}}"; 64 Map<String, UserInfo> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfo>>(){}); 65 System.out.println(map.get("user")); 66 } 67 public static void test5(){ 68 Date date=new Date(); 69 //輸出毫秒值 70 System.out.println(JSON.toJSONString(date)); 71 //默認格式爲yyyy-MM-dd HH:mm:ss 72 System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat)); 73 //根據自定義格式輸出日期 74 System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat)); 75 } 76 public static void main(String[] args) { 77 test1(); 78 test2(); 79 test3(); 80 test4(); 81 test5(); 82 } 83 }
out結果:spa
1 JSON={"age":24,"name":"zhangsan"} 2 JSON==={"age":24,"girlInfo":{"age":"23","name":"xiaohong"},"hobby":["登山","騎車","旅遊"],"sex":"男","username":"zhangsan"} 3 name:chenggang, age:24 4 test.UserInfo@30c221 5 1408190003876 6 "2014-08-16 19:53:23" 7 "2014-08-16"