溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JSON中的fastjson、jackson、gson該如何選擇

發(fā)布時(shí)間:2021-12-18 19:43:52 來(lái)源:億速云 閱讀:226 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家介紹JSON中的fastjson、jackson、gson該如何選擇,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

JSON具有表達(dá)簡(jiǎn)潔、層級(jí)清晰的特點(diǎn),目前廣泛應(yīng)用在數(shù)據(jù)的通信傳輸中,尤其前后端的交互,幾乎都是使用JSON實(shí)現(xiàn)的。例如下面的數(shù)據(jù):

{
 "code" : 0,
 "kind" : "Electronics",
 "list" : [{
   "name" : "computer",
   "price" : 4500,
   "size" : 60
  }, {
   "name" : "iphone",
   "price" : 6000,
   "size" : 55
  }, {
   "name" : "watch",
   "price" : 500,
   "size" : 35
  }
 ]
}

在Java中,JSON的解析方式很多,例如fastjson(阿里)、Gson(谷歌)、jackjson等。

1 fastjson

阿里的fastjson目前是應(yīng)用最廣泛的,在maven項(xiàng)目中的pom依賴:

<dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>fastjson</artifactId>
 <version>1.2.47</version>
</dependency>

直接貼上一些常用的JSON解析方法:

public class Mytest {
 public static void main(String[] args) {
  String goodsData = "{\"code\":0,\"kind\":\"Electronics\",\"list\":[{\"name\":\"computer\",\"price\":4500,\"size\":60},{\"name\":\"iphone\",\"price\":6000,\"size\":55},{\"name\":\"watch\",\"price\":500,\"size\":35}]}";
  String goodsListData = "[{\"name\":\"computer\",\"price\":4500,\"size\":60},{\"name\":\"iphone\",\"price\":6000,\"size\":55}]";
  // 將符合格式的字符串解析成JSONObject
  JSONObject goodsObject = JSON.parseObject(goodsData);
  // 將符合格式的字符串解析成JSONArray
  JSONArray goodsArray = JSON.parseArray(goodsListData);
  // 在JSONObject中獲取JSONArray
  JSONArray goodsList = goodsObject.getJSONArray("list");
  // 在JSONArray中根據(jù)索引獲取JSONObject
  JSONObject goods = goodsList.getJSONObject(0);
  // 在JSONObject中獲取String
  String goodsName = goods.getString("name");
  // 在JSONObject中獲取Integer
  Integer goodsPrice = goods.getInteger("price");

  System.out.println("goodsArray:" + goodsArray);
  System.out.println("goods:" + goods);
  System.out.println("goodsName:" + goodsName);
  System.out.println("goodsPrice:" + goodsPrice);
 }
}

輸出結(jié)果:

goodsArray:[{"name":"computer","price":4500,"size":60},{"name":"iphone","price":6000,"size":55}]
goods:{"name":"computer","price":4500,"size":60}
goodsName:computer
goodsPrice:4500

fastjson目前已支持jsonpath解析:JSONPath

2 jsoncode

jsoncode的maven地址如下:

<dependency>
       <groupId>cn.miludeer</groupId>
       <artifactId>jsoncode</artifactId>
       <version>1.2.4</version>
</dependency>

jsoncode對(duì)于json的解析比起fastjson來(lái),更加直接簡(jiǎn)潔,代碼如下:

import cn.miludeer.jsoncode.JsonCode;

public class Test {
    public static void main(String[] args) {
        String string = "{\"code\" : 0,\"data\" : {\"kind\" : \"Electronics\",\"list\" : [{\"name\" : \"computer\",\"price\" : 4500,\"size\" : 55}, {\"name\" : \"iphone\",\"price\" : 6000,\"size\" : 60}]}}";
        String[] list = JsonCode.getValueList(string, "$.data.list");
        String kind = JsonCode.getValue(string, "$.data.kind");
        System.out.println("list:" + list[1]);
        System.out.println("kind:" + kind);
    }
}

輸出結(jié)果:

list:{"name" : "iphone","price" : 6000,"size" : 60}
kind:Electronics

jsoncode對(duì)比f(wàn)astjson,在便利性上有了很大提升,但仍有局限性,只能單獨(dú)處理JSONArray,無(wú)法處理JSONObject和JSONArray混合的場(chǎng)景。

3 jsonpath

前面兩種json解析都有一定的不足之處,幸好,還有jsonpath這一款神器。首先,它的maven地址是:

<!-- https://mvnrepository.com/artifact/io.gatling/jsonpath -->
<dependency>
    <groupId>io.gatling</groupId>
    <artifactId>jsonpath_2.11</artifactId>
    <version>0.6.4</version>
</dependency>

準(zhǔn)備如下的JSON測(cè)試數(shù)據(jù):

{
 "code" : 0,
 "data" : {
  "kind" : "Electronics",
  "list" : [{
    "name" : "computer",
    "price" : 4500,
    "size" : 55
   }, {
    "name" : "iphone",
    "price" : 6000,
    "size" : 60
   }, {
    "name" : "watch",
    "price" : 8000,
    "size" : 30
   }
  ]
 }
}

jsonpath提供了非常豐富便捷的解析表達(dá)式,以上面的json串為例,演示幾個(gè)示例:

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;

import java.util.List;

/**
 * @author guozhengMu
 * @version 1.0
 * @date 2019/3/26 18:38
 * @description
 * @modify
 */
public class Test {
    public static void main(String[] args) {
        String string = "{\"code\" : 0,\"data\" : {\"kind\" : \"Electronics\",\"list\" : [{\"name\" : \"computer\",\"price\" : 4500,\"size\" : 55}, {\"name\" : \"iphone\",\"price\" : 6000,\"size\" : 60},{\"name\" : \"watch\",\"price\" : 8000,\"size\" : 30}]}}";

        ReadContext context = JsonPath.parse(string);
        // 獲取單個(gè)值
        String name = context.read("$.data.list[0].name");
        // 獲取JSONArray所有name
        List<String> names = context.read("$.data.list[*].name");
        // 獲取0、2
        List<String> names2 = context.read("$.data.list[0,2].name");
        // 獲取0-2(不含2)
        List<String> names3 = context.read("$.data.list[0:2].name");

        System.out.println("name:" + name);
        System.out.println("names:" + names);
        System.out.println("names2:" + names2);
        System.out.println("names3:" + names3);
    }
}

輸出結(jié)果:

 name:computer
 names:["computer","iphone","watch"]
 names2:["computer","watch"]
 names3:["computer","iphone"]

表達(dá)式匯總:

序號(hào)表達(dá)式輸出結(jié)果作用
1$.data.list[0].nameString:computer獲取單個(gè)value
2$.data.list[*].nameList:[“computer”,“iphone”,“watch”]獲取全部value
3$.data.list[0,2].nameList:[“computer”,“watch”]獲取特定索引的value
4$.data.list[1:].nameList:[“iphone”,“watch”]獲取索引之后的所有value(含該索引)
5$.data.list[:2].nameList:[“computer”,“iphone”]獲取索引之前的所有value(不含該索引)
6$.data.list[?(@.price>6500)]List:[{“name”:“iphone”,“price”:6000,“size”:60},{“name”:“watch”,“price”:8000,“size”:30}]根據(jù)條件篩選
7$.data.list[?(@.name == ‘computer')][{“name”:“computer”,“price”:4500,“size”:55}]根據(jù)條件篩選
8$.data.list[?(@.name)]List:[{“name”:“computer”,“price”:4500,“size”:55},{“name”:“iphone”,“price”:6000,“size”:60},{“name”:“watch”,“price”:8000,“size”:30}]獲取含有name屬性的元素
9$.data.list[?(@.price > 5000 && @.size > 30)]List:[{“name”:“iphone”,“price”:6000,“size”:60}]多條件查詢(且)
10$.data.list[?(@.price < 7000 || @.size <= 30)]List:[{“name”:“iphone”,“price”:6000,“size”:60},{“name”:“watch”,“price”:8000,“size”:30}]多條件查詢(或)
11$.data.list.length()Integer:3查詢JSONArray長(zhǎng)度
12$.max($.data.list[0].price,$.data.list[1].price)Object:6000.0獲取最大值,最小值:min,平均值:avg,標(biāo)準(zhǔn)差:stddev
13$.data.list[?(@.price > 5000 && @.size > 30)]List:[{“name”:“iphone”,“price”:6000,“size”:60}]多條件查詢(且)

關(guān)于JSON中的fastjson、jackson、gson該如何選擇就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI