Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to choose fastjson, jackson and gson in JSON

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to choose fastjson, jackson and gson in JSON. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

JSON has the characteristics of concise expression and clear hierarchy. At present, it is widely used in data communication and transmission, especially the interaction between front and rear ends, almost all of which are realized by JSON. For example, the following data:

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

In Java, there are many ways to parse JSON, such as fastjson (Ali), Gson (Google), jackjson and so on.

1 fastjson

Ali's fastjson is currently the most widely used, and pom in the maven project depends on:

Com.alibaba fastjson 1.2.47

Post some commonly used JSON parsing methods directly:

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\ ": \ "size\": 35}]} " String goodsListData = "[{\" name\ ":\" computer\ ",\" price\ ": 4500,\" size\ ": 60}, {\" name\ ":\" iphone\ ",\" price\ ": 6000,\" size\ ": 55}]"; / / parse the formatted string into JSONObject JSONObject goodsObject = JSON.parseObject (goodsData); / / parse the formatted string into JSONArray JSONArray goodsArray = JSON.parseArray (goodsListData) / / get JSONArray JSONArray goodsList = goodsObject.getJSONArray ("list") in JSONObject; / / get JSONObject JSONObject goods = goodsList.getJSONObject (0) in JSONArray according to the index; / / get String String goodsName = goods.getString ("name") in JSONObject; / / get Integer Integer goodsPrice = goods.getInteger ("price") in JSONObject; System.out.println ("goodsArray:" + goodsArray); System.out.println ("goods:" + goods) System.out.println ("goodsName:" + goodsName); System.out.println ("goodsPrice:" + goodsPrice);}}

Output result:

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 currently supports jsonpath parsing: JSONPath

2 jsoncode

The maven address of jsoncode is as follows:

Cn.miludeer jsoncode 1.2.4

Jsoncode parses json more directly and succinctly than fastjson. The code is as follows:

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);}}

Output result:

List: {"name": "iphone", "price": 6000, "size": 60}

Kind:Electronics

Compared with fastjson, jsoncode has a great improvement in convenience, but it still has limitations. It can only deal with JSONArray alone, but can not deal with the mixed scenarios of JSONObject and JSONArray.

3 jsonpath

The first two kinds of json parsing have some shortcomings, but fortunately, there is an artifact called jsonpath. First of all, its maven address is:

Io.gatling jsonpath_2.11 0.6.4

Prepare the following JSON test data:

{"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 provides rich and convenient parsing expressions. Take the above json string as an example to demonstrate several examples:

Import com.jayway.jsonpath.JsonPath;import com.jayway.jsonpath.ReadContext;import java.util.List / * @ author guozhengMu * @ version 1.0 * @ date on 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); / / get a single value String name = context.read ("$.data.list [0] .name"); / / get all JSONArray name List names = context.read ("$.data.list [*] .name"); / / get 0,2 List names2 = context.read ("$.data.list [0L2] .name") / get 0-2 (excluding 2) List 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);}}

Output result:

Name:computer

Names: ["computer", "iphone", "watch"]

Names2: ["computer", "watch"]

Names3: ["computer", "iphone"]

Expression summary:

The ordinal expression output result functions as 1 $.data.list [0] .nameString: computer gets a single value2 $.data.list [*] .nameList: ["computer", "iphone", "watch"] gets all value3 $.data.list [0force 2] .nameList: ["computer", "watch"] gets value4 $.data.list [1:] .nameList: ["iphone" "watch"] gets all value (including the index) 5 $.data.list [: 2] .nameList: ["computer", "iphone"] gets all value before the index (excluding the index) 6 $.data.list [? (@ .price > 6500)] List: [{"name": "iphone", "price": 6000, "size": 60}, {"name": "watch", "price": 8000 "size": 30}] filter 7 $.data.list [? (@ .name = = 'computer')] [{"name": "computer", "price": 4500, "size": 55}] filter 8 $.data.list [? (@ .name)] List: [{"name": "computer", "price": 4500, "size": 55}, {"name": "iphone", "price": 6000, "size": 60}, {"name": "watch" "price": 8000, "size": 30}] get the element 9 $.data.list [? (@ .price > 5000 & & @ .size > 30)] List: [{"name": "iphone", "price": 6000, "size": 60}] Multi-condition query (and) 10 $.data.list [? (@ .price)

< 7000 || @.size 5000 && @.size >

30)] List: [{"name": "iphone", "price": 6000, "size": 60}] Multi-conditional query (and) this is how to choose fastjson, jackson and gson in JSON. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report