json path

import com.google.common.collect.Lists;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;

import java.util.List;

/**
 * @author shiqil.liu
 * @date 2019-08-22 10:41
 */
public class DealJsonPath {
    public static void main(String[] args) {
        String json = "{
" +
                "    "status":0,
" +
                "    "message":"ok",
" +
                "    "results":[
" +
                "        {
" +
                "            "name":"泰山风景名胜区",
" +
                "            "location":{
" +
                "                "lat":36.264227,
" +
                "                "lng":117.086616
" +
                "            },
" +
                "            "address":"泰安市岱岳区东岳大街西段501号",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"岱岳区",
" +
                "            "street_id":"ea4a3dc942c4b72f4f5547aa",
" +
                "            "telephone":"(0538)96008888",
" +
                "            "detail":1,
" +
                "            "uid":"ea4a3dc942c4b72f4f5547aa"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山风景名胜区-南天门",
" +
                "            "location":{
" +
                "                "lat":36.262014,
" +
                "                "lng":117.110857
" +
                "            },
" +
                "            "address":"泰安市泰山区红门路54号泰山风景名胜区内",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"泰山区",
" +
                "            "detail":1,
" +
                "            "uid":"a3e8341c5497a7964afd3f55"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山站",
" +
                "            "location":{
" +
                "                "lat":36.193518,
" +
                "                "lng":117.116341
" +
                "            },
" +
                "            "address":"泰安市泰山区龙潭路",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"泰山区",
" +
                "            "street_id":"4de99463948347e088b988b3",
" +
                "            "telephone":"(0538)2181747",
" +
                "            "detail":1,
" +
                "            "uid":"4de99463948347e088b988b3"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山温泉城",
" +
                "            "location":{
" +
                "                "lat":36.079672,
" +
                "                "lng":117.23363
" +
                "            },
" +
                "            "address":"山东省泰安市高新区徂徕镇郑家庄村",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"岱岳区",
" +
                "            "street_id":"cff95196212daf6c9f09f73c",
" +
                "            "detail":1,
" +
                "            "uid":"cff95196212daf6c9f09f73c"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山学院",
" +
                "            "location":{
" +
                "                "lat":36.228513,
" +
                "                "lng":117.045757
" +
                "            },
" +
                "            "address":"泰安市岱岳区东岳大街525号",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"岱岳区",
" +
                "            "street_id":"2c0bd6c5f06b4cd643ab9ae7",
" +
                "            "telephone":"(0538)6715631,(0538)6715599",
" +
                "            "detail":1,
" +
                "            "uid":"2c0bd6c5f06b4cd643ab9ae7"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山学院(南校区)",
" +
                "            "location":{
" +
                "                "lat":36.138018,
" +
                "                "lng":117.083513
" +
                "            },
" +
                "            "address":"泰安市岱岳区凤天路789号",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"岱岳区",
" +
                "            "street_id":"1816533484df9705fa2d61a4",
" +
                "            "telephone":"(0538)6711231",
" +
                "            "detail":1,
" +
                "            "uid":"1816533484df9705fa2d61a4"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山锦绣谷",
" +
                "            "location":{
" +
                "                "lat":36.311547,
" +
                "                "lng":117.161553
" +
                "            },
" +
                "            "address":"进宫沟附近",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"泰山区",
" +
                "            "detail":1,
" +
                "            "uid":"4edf9fbfda0665e9cc06a4e8"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山区",
" +
                "            "location":{
" +
                "                "lat":36.199445,
" +
                "                "lng":117.141411
" +
                "            },
" +
                "            "address":"山东省泰安市",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"泰山区",
" +
                "            "detail":0,
" +
                "            "uid":"d614eca365df78a85a1093fc"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山新兴园",
" +
                "            "location":{
" +
                "                "lat":36.204916,
" +
                "                "lng":117.072936
" +
                "            },
" +
                "            "address":"粥店街道长城西路69号",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"岱岳区",
" +
                "            "street_id":"a6f8707170120c2903ff2d66",
" +
                "            "detail":1,
" +
                "            "uid":"a6f8707170120c2903ff2d66"
" +
                "        },
" +
                "        {
" +
                "            "name":"泰山金华大厦",
" +
                "            "location":{
" +
                "                "lat":36.189219,
" +
                "                "lng":117.105924
" +
                "            },
" +
                "            "address":"泰安市泰山区泰山大街326号",
" +
                "            "province":"山东省",
" +
                "            "city":"泰安市",
" +
                "            "area":"泰山区",
" +
                "            "street_id":"8036a74b25b5d6944e6d46b1",
" +
                "            "detail":1,
" +
                "            "uid":"8036a74b25b5d6944e6d46b1"
" +
                "        }
" +
                "    ]
" +
                "}";

        DocumentContext doc = JsonPath.parse(json, Configuration.builder().options(Option.ALWAYS_RETURN_LIST).build());
        List list = doc.read("$.results[*][?(@.telephone)].location.*");
        List<String> result = Lists.newArrayListWithCapacity(list.size());
        for (Object data : list) {
            if (data instanceof String) {
                result.add((String)data);
            } else if (data != null){
                result.add(data.toString());
            }
        }
        result.forEach(System.out::println);
    }
}
原文地址:https://www.cnblogs.com/TheQi/p/11393563.html