Map<String, String> map ----> $.store.bicycle
String str = $.store.other
List<Map<String, String>> list = $.store.book
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}Map<String, String> entries = JsonPath.read(str, "$");
JSONPATH Parse JSON array file:
If the object is:
[
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4],
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
]
Then "$[0]" will return:
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4]
And "$[1]" will return:
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
You can do it two levels deep as well. "$[0][4]" will return:
205
You can also extract the elements of the array into a list with "$[*]", which will return a list of 2 elements. The first being:
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4]
and the second being:
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
} "other":"[{\"filterable\":\"true\",\"dataIndex\":\"DateValue\",\"dataType\":\"date\",\"isInStandardView\":\"true\",\"sortAsConverterFormat\":\"yyyyMMdd\",\"id\":\"DateValue\",\"sortable\":\"true\",\"isVisible\":\"true\"},{\"filterable\":\"true\",\"dataIndex\":\"DateType\",\"dataType\":\"string\",\"isInStandardView\":\"true\",\"sortAsConverterFormat\":null,\"id\":\"DateType\",\"sortable\":\"true\",\"isVisible\":\"true\"}]"
},
"expensive": 10
}

{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"isbn": "0-553-21311-3"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
private static void jsonPathTest() {
JSONObject json = jsonTest();//调用自定义的jsonTest()方法获得json对象,生成上面的json
//输出book[0]的author值
String author = JsonPath.read(json, "$.store.book[0].author");
//输出全部author的值,使用Iterator迭代
List<String> authors = JsonPath.read(json, "$.store.book[*].author");
//输出book[*]中category == 'reference'的book
List<Object> books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");
//输出book[*]中price>10的book
List<Object> books = JsonPath.read(json, "$.store.book[?(@.price>10)]");
//输出book[*]中含有isbn元素的book
List<Object> books = JsonPath.read(json, "$.store.book[?(@.isbn)]");
//输出该json中所有price的值
List<Double> prices = JsonPath.read(json, "$..price");
//可以提前编辑一个路径,并多次使用它
JsonPath path = JsonPath.compile("$.store.book[*]");
List<Object> books = path.read(json);
}
来源:https://www.cnblogs.com/zi-yao/p/6542949.html