日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Json解析工具对比

發(fā)布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Json解析工具对比 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一 、各個JSON技術(shù)的簡介和優(yōu)劣

1.json-lib

json-lib最開始的也是應用最廣泛的json解析工具,json-lib 不好的地方確實是依賴于很多第三方包,
包括commons-beanutils.jar,commons-collections-3.2.jar,commons-lang-2.6.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar,
對于復雜類型的轉(zhuǎn)換,json-lib對于json轉(zhuǎn)換成bean還有缺陷,比如一個類里面會出現(xiàn)另一個類的list或者map集合,json-lib從json到bean的轉(zhuǎn)換就會出現(xiàn)問題。
json-lib在功能和性能上面都不能滿足現(xiàn)在互聯(lián)網(wǎng)化的需求。

2.開源的Jackson

相比json-lib框架,Jackson所依賴的jar包較少,簡單易用并且性能也要相對高些。
而且Jackson社區(qū)相對比較活躍,更新速度也比較快。
Jackson對于復雜類型的json轉(zhuǎn)換bean會出現(xiàn)問題,一些集合Map,List的轉(zhuǎn)換出現(xiàn)問題。
Jackson對于復雜類型的bean轉(zhuǎn)換Json,轉(zhuǎn)換的json格式不是標準的Json格式

3.Google的Gson

Gson是目前功能最全的Json解析神器,Gson當初是為因應Google公司內(nèi)部需求而由Google自行研發(fā)而來,
但自從在2008年五月公開發(fā)布第一版后已被許多公司或用戶應用。
Gson的應用主要為toJson與fromJson兩個轉(zhuǎn)換函數(shù),無依賴,不需要例外額外的jar,能夠直接跑在JDK上。
而在使用這種對象轉(zhuǎn)換之前需先創(chuàng)建好對象的類型以及其成員才能成功的將JSON字符串成功轉(zhuǎn)換成相對應的對象。
類里面只要有g(shù)et和set方法,Gson完全可以將復雜類型的json到bean或bean到json的轉(zhuǎn)換,是JSON解析的神器。
Gson在功能上面無可挑剔,但是性能上面比FastJson有所差距。

4.阿里巴巴的FastJson

Fastjson是一個Java語言編寫的高性能的JSON處理器,由阿里巴巴公司開發(fā)。
無依賴,不需要例外額外的jar,能夠直接跑在JDK上。
FastJson在復雜類型的Bean轉(zhuǎn)換Json上會出現(xiàn)一些問題,可能會出現(xiàn)引用的類型,導致Json轉(zhuǎn)換出錯,需要制定引用。
FastJson采用獨創(chuàng)的算法,將parse的速度提升到極致,超過所有json庫。

綜上4種Json技術(shù)的比較,在項目選型的時候可以使用Google的Gson和阿里巴巴的FastJson兩種并行使用,
如果只是功能要求,沒有性能要求,可以使用google的Gson,

如果有性能上面的要求可以使用Gson將bean轉(zhuǎn)換json確保數(shù)據(jù)的正確,使用FastJson將Json轉(zhuǎn)換Bean

二。使用簡介

1、Google的Gson包的使用簡介。

Gson類:解析json的最基礎(chǔ)的工具類
JsonParser類:解析器來解析JSON到JsonElements的解析樹
JsonElement類:一個類代表的JSON元素
JsonObject類:JSON對象類型
JsonArray類:JsonObject數(shù)組
TypeToken類:用于創(chuàng)建type,比如泛型List<?>

(1)maven依賴


com.google.code.gson
gson
2.2.4

(2)基礎(chǔ)轉(zhuǎn)換類

[java]?view plaincopy
  • public?class?Book{??
  • private?String?id;??
  • private?String?name;??
  • public?Book()?{??
  • super();??
  • }??
  • public?String?getId()?{??
  • return?id;??
  • }??
  • public?void?setId(String?id)?{??
  • this.id?=?id;??
  • }??
  • public?String?getName()?{??
  • return?name;??
  • }??
  • public?void?setName(String?name)?{??
  • this.name?=?name;??
  • }??
  • }??
  • public?class?Student{??
  • private?String?name;??
  • private?int?age;??
  • private?String?sex;??
  • private?String?describe;??
  • private?Set?books;??
  • public?Student()?{??
  • super();??
  • }??
  • public?String?getName()?{??
  • return?name;??
  • }??
  • public?void?setName(String?name)?{??
  • this.name?=?name;??
  • }??
  • public?int?getAge()?{??
  • return?age;??
  • }??
  • public?void?setAge(int?age)?{??
  • this.age?=?age;??
  • }??
  • public?String?getSex()?{??
  • return?sex;??
  • }??
  • public?void?setSex(String?sex)?{??
  • this.sex?=?sex;??
  • }??
  • public?Set?getBooks()?{??
  • return?books;??
  • }??
  • public?void?setBooks(Set?books)?{??
  • this.books?=?books;??
  • }??
  • public?String?getDescribe()?{??
  • return?describe;??
  • }??
  • public?void?setDescribe(String?describe)?{??
  • this.describe?=?describe;??
  • }??
  • }??

  • (3)bean轉(zhuǎn)換json

    Gson gson = new Gson();
    String json = gson.toJson(obj);
    obj是對象

    (4)json轉(zhuǎn)換bean

    Gson gson = new Gson();
    String json = "{\"id\":\"2\",\"name\":\"Json技術(shù)\"}";
    Book book = gson.fromJson(json, Book.class);

    (5)json轉(zhuǎn)換復雜的bean,比如List,Set

    將json轉(zhuǎn)換成復雜類型的bean,需要使用TypeToken
    Gson gson = new Gson();
    String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
    //將json轉(zhuǎn)換成List
    List list = gson.fromJson(json,new TypeToken<LIST>() {}.getType());
    //將json轉(zhuǎn)換成Set
    Set set = gson.fromJson(json,new TypeToken<SET>() {}.getType());

    (6)通過json對象直接操作json以及一些json的工具

    a)格式化Json

    String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    JsonParser jp = new JsonParser();
    JsonElement je = jp.parse(json);
    json = gson.toJson(je);

    b)判斷字符串是否是json,通過捕捉的異常來判斷是否是json

    String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
    boolean jsonFlag;
    try {
    new JsonParser().parse(str).getAsJsonObject();
    jsonFlag = true;
    } catch (Exception e) {
    jsonFlag = false;
    }

    c)從json串中獲取屬性

    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    String propertyName = 'id';
    String propertyValue = "";
    try {
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(json);
    JsonObject jsonObj = element.getAsJsonObject();
    propertyValue = jsonObj.get(propertyName).toString();
    } catch (Exception e) {
    propertyValue = null;
    }

    d)除去json中的某個屬性

    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    String propertyName = 'id';
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(json);
    JsonObject jsonObj = element.getAsJsonObject();
    jsonObj.remove(propertyName);
    json = jsonObj.toString();

    e)向json中添加屬性

    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    String propertyName = 'desc';
    Object propertyValue = "json各種技術(shù)的調(diào)研";
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(json);
    JsonObject jsonObj = element.getAsJsonObject();
    jsonObj.addProperty(propertyName, new Gson().toJson(propertyValue));

    json = jsonObj.toString();

    f)修改json中的屬性

    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    String propertyName = 'name';
    Object propertyValue = "json各種技術(shù)的調(diào)研";
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(json);
    JsonObject jsonObj = element.getAsJsonObject();
    jsonObj.remove(propertyName);
    jsonObj.addProperty(propertyName, new Gson().toJson(propertyValue));
    json = jsonObj.toString();

    g)判斷json中是否有屬性

    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    String propertyName = 'name';
    boolean isContains = false ;
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(json);
    JsonObject jsonObj = element.getAsJsonObject();
    isContains = jsonObj.has(propertyName);

    h)json中日期格式的處理

    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Gson gson = builder.create();
    然后使用gson對象進行json的處理,如果出現(xiàn)日期Date類的對象,就會按照設置的格式進行處理
    i)json中對于Html的轉(zhuǎn)義
    Gson gson = new Gson();
    這種對象默認對Html進行轉(zhuǎn)義,如果不想轉(zhuǎn)義使用下面的方法
    GsonBuilder builder = new GsonBuilder();
    builder.disableHtmlEscaping();
    Gson gson = builder.create();

    2、阿里巴巴的FastJson包的使用簡介。

    (1)maven依賴


    com.alibaba
    fastjson
    1.1.22

    (2)基礎(chǔ)轉(zhuǎn)換類

    同上

    (3)bean轉(zhuǎn)換json

    1.將對象轉(zhuǎn)換成格式化的json
    JSON.toJSONString(obj,true);

    2.將對象轉(zhuǎn)換成非格式化的json

    JSON.toJSONString(obj,false);

    3.對于復雜類型的轉(zhuǎn)換,對于重復的引用在轉(zhuǎn)成json串后在json串中出現(xiàn)引用的字符,比如 $ref":"$[0].books[1]Student stu = new Student();

    Set books= new HashSet();
    Book book = new Book();
    books.add(book);
    stu.setBooks(books);
    List list = new ArrayList();
    for(int i=0;i<5;i++)
    list.add(stu);

    String json = JSON.toJSONString(list,true);

    一般的列表/數(shù)組轉(zhuǎn)json

    List<FastEntity> fastList = new ArrayList<>(); for (int i = 0; i < 3; i++) { FastEntity fast = new FastEntity(i, "" + i); fastList.add(fast); Log.e("resultttt", "bean-->json結(jié)果" + JSON.toJSONString(fast)); } Log.e("resultttt", "list-->json結(jié)果" + JSON.toJSONString(fastList)); Log.e("resultttt", "arrsy-->json結(jié)果" + JSON.toJSONString(fastList.toArray())); Log.e("resultttt", "list-->json結(jié)果" + JSON.toJSONString(Arrays.asList(fastList.toArray())));

    (4)json轉(zhuǎn)換bean

    String json = "{\"id\":\"2\",\"name\":\"Json技術(shù)\"}";
    Book book = JSON.parseObject(json, Book.class);

    (5)json轉(zhuǎn)換復雜的bean,比如List,Map

    String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
    //將json轉(zhuǎn)換成List
    List list = JSON.parseObject(json,new TypeReference<ARRAYLIST>(){});
    //將json轉(zhuǎn)換成Set
    Set set = JSON.parseObject(json,new TypeReference<HASHSET>(){});

    (6)通過json對象直接操作json

    a)從json串中獲取屬性

    String propertyName = 'id';
    String propertyValue = "";
    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    JSONObject obj = JSON.parseObject(json);
    propertyValue = obj.get(propertyName));

    b)除去json中的某個屬性

    String propertyName = 'id';
    String propertyValue = "";
    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    JSONObject obj = JSON.parseObject(json);
    Set set = obj.keySet();
    propertyValue = set.remove(propertyName);

    json = obj.toString();

    c)向json中添加屬性

    String propertyName = 'desc';
    Object propertyValue = "json的玩意兒";
    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    JSONObject obj = JSON.parseObject(json);
    obj.put(propertyName, JSON.toJSONString(propertyValue));

    json = obj.toString();

    d)修改json中的屬性

    String propertyName = 'name';
    Object propertyValue = "json的玩意兒";
    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    JSONObject obj = JSON.parseObject(json);
    Set set = obj.keySet();
    if(set.contains(propertyName))
    obj.put(propertyName, JSON.toJSONString(propertyValue));

    json = obj.toString();

    e)判斷json中是否有屬性

    String propertyName = 'name';
    boolean isContain = false;
    String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
    JSONObject obj = JSON.parseObject(json);
    Set set = obj.keySet();
    isContain = set.contains(propertyName);

    f)json中日期格式的處理

    Object obj = new Date();
    String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss.SSS");
    使用JSON.toJSONStringWithDateFormat,該方法可以使用設置的日期格式對日期進行轉(zhuǎn)換

    總結(jié)

    以上是生活随笔為你收集整理的Json解析工具对比的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。