Javaシリーズ(2):データ構造のJSONの概要
26926 ワード
データ構造のJSONの浅い分析
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
}
]
}
Javaでは,JSONの解析方式が多く,たとえばfastjson(アリ),Gson(Google),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\",\"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 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","price":4500,"size":60}
goodsName:computer
goodsPrice:4500
fastjsonの解析方法は層ごとに解析するしかなく,使いにくい.ただし、fastjsonを使用することに慣れている学生は、fastjson解析方法に基づいて、ツール方法をカプセル化し、「$.list[0]」を使用して要素を抽出することができます.具体的なコードはhttps://blog.csdn.net/mu_wind/article/details/93124113この文章の末尾.
2 jsoncode
jsoncodeのmavenアドレスは以下の通りです.<dependency>
<groupId>cn.miludeer</groupId>
<artifactId>jsoncode</artifactId>
<version>1.2.4</version>
</dependency>
jsoncodeのjsonに対する解析はfastjsonよりも直接的で簡潔であり,コードは以下の通りである.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","price" : 6000,"size" : 60}
kind:Electronics
jsoncodeはfastjsonと比較して利便性が大きく向上したが,まだ限界があり,JSOnArray単独でしか処理できず,JSOnObjectとJSOnArrayが混在するシーンは処理できない.
3 jsonpath
前の2つの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",
"price" : 4500,
"size" : 55
}, {
"name" : "iphone",
"price" : 6000,
"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,\"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);
//
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);
}
}
出力結果: 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”,“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長さ
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”,“price”:6000,“size”:60}]
複数条件クエリー(および)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
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 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","price":4500,"size":60}
goodsName:computer
goodsPrice:4500
jsoncodeのmavenアドレスは以下の通りです.
<dependency>
<groupId>cn.miludeer</groupId>
<artifactId>jsoncode</artifactId>
<version>1.2.4</version>
</dependency>
jsoncodeのjsonに対する解析はfastjsonよりも直接的で簡潔であり,コードは以下の通りである.
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","price" : 6000,"size" : 60}
kind:Electronics
jsoncodeはfastjsonと比較して利便性が大きく向上したが,まだ限界があり,JSOnArray単独でしか処理できず,JSOnObjectとJSOnArrayが混在するシーンは処理できない.
3 jsonpath
前の2つの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",
"price" : 4500,
"size" : 55
}, {
"name" : "iphone",
"price" : 6000,
"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,\"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);
//
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);
}
}
出力結果: 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”,“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長さ
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”,“price”:6000,“size”:60}]
複数条件クエリー(および)
<!-- https://mvnrepository.com/artifact/io.gatling/jsonpath -->
<dependency>
<groupId>io.gatling</groupId>
<artifactId>jsonpath_2.11</artifactId>
<version>0.6.4</version>
</dependency>
{
"code" : 0,
"data" : {
"kind" : "Electronics",
"list" : [{
"name" : "computer",
"price" : 4500,
"size" : 55
}, {
"name" : "iphone",
"price" : 6000,
"size" : 60
}, {
"name" : "watch",
"price" : 8000,
"size" : 30
}
]
}
}
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);
//
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);
}
}
name:computer
names:["computer","iphone","watch"]
names2:["computer","watch"]
names3:["computer","iphone"]