FastJson使用示例
這幾天在網上找關于Json的一些案例,無意當中找到了一個我個人感覺比較好的就是阿里巴巴工程師寫的FastJson。
package com.jerehedu.fastjson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jerehedu.entity.Userinfo;
/**
* fastjson 是一個性能很好的 Java 語言實現的 JSON 解析器和生成器,來自阿里巴巴的工程師開發。 主要特點:
* 1.快速FAST(比其它任何基于Java的解析器和生成器更快,包括jackson) 強大(支持普通JDK類包括任意Java Bean
* 2.Class、Collection、Map、Date或enum) 零依賴(沒有依賴其它任何類庫除了JDK)
*
*/
public class TestFastJson {
public static void main(String[] args) {
String json = "{\"name\":\"chenggang\",\"age\":24}";
String arrayAyy = "[[\'馬云',50],null,[\'馬化騰',30]]";
Entity2json("zhangsan", 24);
list2Json();
Complexdata();
Deserialization(json);
DateFormate(new Date());
Json2Eetity(json);
String2JSONArray(arrayAyy);
}
// 實體轉為Json
public static void Entity2json(String name, int age) {
Userinfo info = new Userinfo(name, age);
String str_json = JSON.toJSONString(info); //
System.out.println("實體轉化為Json" + str_json);
}運行結果為:
直接將我們的實體轉化為了Json格式。
// list轉Json
public static void list2Json() {
List<Userinfo> list = new ArrayList<Userinfo>();
Userinfo userinfo1 = new Userinfo("lisi", 15);
Userinfo userinfo2 = new Userinfo("wangwu", 16);
list.add(userinfo1);
list.add(userinfo2);
String json = JSON.toJSONString(list, true);
System.out.println("List集合轉json格式字符串 :" + json);
}
運行結果為:
// 字符數組轉化為JSon
private static void String2JSONArray(String arrayAyy) {
JSONArray array = JSONArray.parseArray(arrayAyy);
System.out.println("數組:" + array);
System.out.println("數組長度: " + array.size());
Collection nuCon = new Vector();
nuCon.add(null);
array.removeAll(nuCon);
System.out.println("數組:" + array);
System.out.println("數組長度: " + array.size());
}運行結果為:
// 復雜數據類型
public static void Complexdata() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("username", "zhangsan");
map.put("age", 24);
map.put("sex", "男");
// map集合
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", "xiaohong");
temp.put("age", "23");
map.put("girlInfo", temp);
// list集合
List<String> list = new ArrayList<String>();
list.add("爬山");
list.add("騎車");
list.add("旅游");
map.put("hobby", list);
String jsonString = JSON.toJSONString(map);
System.out.println("復雜數據類型:" + jsonString);
}運行結果為:
復雜數據類型:{"age":24,"girlInfo":{"age":"23","name":"xiaohong"},"hobby":["爬山","騎車","旅游"],"sex":"男","username":"zhangsan"}
public static void Deserialization(String json) { Userinfo userInfo = JSON.parseObject(json, Userinfo.class); System.out.println("姓名是:" + userInfo.getName() + ", 年齡是:" + userInfo.getAge()); }
解析Json字符串,運行結果為:
// 格式話日期
public static void DateFormate(Date date) {
System.out.println("輸出毫秒值:" + JSON.toJSONString(date));
System.out.println("默認格式為:"
+ JSON.toJSONString(date,
SerializerFeature.WriteDateUseDateFormat));
System.out.println("自定義日期:"
+ JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd",
SerializerFeature.WriteDateUseDateFormat));
}格式化日期,運行結果為:
// Json轉為實體
private static void Json2Eetity(String json) {
Userinfo userInfo = JSON.parseObject(json, Userinfo.class);
System.out.println("輸出對象的地址:" + userInfo.toString());
System.out.println("輸出對象的名字:" + userInfo.getName());
}
}運行結果為:
FastJson還是比較好用的一個解析格式,不管是轉化為Json,還是解析Json。Github地址為https://github.com/AlibabaTech/fastjson。
本文由用戶 ybny 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!