JSON中fastjson、jackson、gson如何選擇
目錄
- 1 faston
- 2 jsoncode
- 3 jsonpath
JSON具有表達簡潔、層級清晰的特點,目前廣泛應用在資料的通訊傳輸中,尤其前後端的互動,幾乎都是使用JSON實現的。例如下面的資料:
{ "code" : 0,"kind" : "Electronics","list" : [{ "name" : "computer","price" : 4500,"size" : 60 },{ "name" : "iphone","price" : 6000,"size" : 55 },{ "name" : "watch","price" : 500,"size" : 35 } ] }
在中,JSON的解析方式很多,例如fastjson(阿里)、Gson(谷歌)、jackjson等。
1 fastjson
阿里的fastjson目前是應用最廣泛的,在maven專案中的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\",\"size\":55}]"; // 將符合格式的字串解析成JSONObject JSONObject goodsObject = JSON.parseObject(goodsData); // 將符合格式的字串解析成JSONArray JSONArray goodsArray = JSON.parseArray(goodsListData); // 在JSONObjecthttp://www.cppcns.com中獲取JSONArray JSONArray goodsList = goodsObject.getJSONArray("list"); // 在JSONArray中根據索引獲取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); } }
輸出結果:
goodsArray:[{"name":"computer","price":4500,"size":60},{"name":"iphone","price":6000,"size":55}]
goods:{"name":"computer","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對於json的解析比起fhttp://www.cppcns.comastjson來,更加直接簡潔,程式碼如下:
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); } }
輸出結果:
list:{"name" : "iphone","size" : 60}
kind:Electronics
jsoncode對比fastjson,在便利性上有了很大提升,但仍有侷限性,只能單獨處理JSONArray,無法處理JSONObject和JSONArray混合的場景。
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>
準備如下的JSON測試資料:
{ "code" : 0,"data" : { "kind" : "Electronics","list" : [{ "name" : "computer","size" : 55 },{ "name" : "iphone","size" : 60 },{ "name" : "watch","price" : 8000,"size" : 30 } ] } }
jsonpath提供了非常豐富便捷的解析表示式,以上面的json串為例,演示幾個示例:
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,\"price\" : 6000GTrLxcpA,\"size\" : 60},{\"name\" : \"watch\",\"price\" : 8000,\"size\" : 30}]}}"; ReadContext context = JsonPath.parse(string); // 獲取單個值 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[0http://www.cppcns.com,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); } }
輸出結果:
name:computer
names:["computer","iphone","watch"]
names2:["computer","watch"]
names3:["computer","iphone"]
表示式彙總:
序號 | 表示式 | 輸出結果 | 作用 |
---|---|---|---|
1 | $.data.list[0].name | String:computer | 獲取單個value |
2 | $.data.list[*].name | List:[“computer”,“iphone”,“watch”] | 獲取全部value |
3 | $.data.list[0,2].name | List:[“computer”,“watch”] | 獲取特定索引的value |
4 | $.data.list[1:].name | List:[“iphone”,“watch”] | 獲取索引之後的所有value(含該索引) |
5 | $.data.list[:2].name | List:[“computer”,“iphone”] | 獲取索引之前的所有value(不含該索引) |
6 | $.data.list[?(@.price>6500)] | List:[{“name”:“iphone”,“price”:6000,“size”:60},{“name”:“watch”,“price”:8000,“size”:30}] | 根據條件篩選 |
7 | $.data.list[?(@.name == ‘computer')] | [{“name”:“computer”,“price”:4500,“size”:55}] | 根據條件篩選 |
8 | $.data.list[?(@.name)] | List:[{“name”:“computer”,“size”:55},{“name”:“iphone”,“size”:30}] | 獲取含有name屬性的元素 |
9 | $.data.list[?(@.price > 5000 && @.size > 30)] | List:[{“name”:“iphone”,“size”:60}] | 多條件查詢(且) |
10 | $.data.list[?(@.price < 7000 || @.size <= 30)] | List:[{“name”:“iphone”,“size”:30}] | 多條件查詢(或) |
11 | $.data.list.length() | Integer:3 | 查詢JSONArray長度 |
12 | $.max($.data.list[0].price,$.data.list[1].price) | Object:6000.0 | 獲取最大值,最小值:min,平均值:avg,標準差:stddev |
13 | $.data.list[?(@.price > 5000 && @.size > 30)] | List:[{“name”:“iphone”,“size”:60}] | 多條件查詢(且) |
到此這篇關於JSON中fastjson、jackson、gson如何選擇的文章就介紹到這了,更多相關fastjson jackson gson內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!