zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    性能篇系列—stream详解
    Java正则表达式详细解析
    干货系列性能篇之——序列化
    面试官之问:知道你的接口“QPS”是多少吗?
    Java性能之优化RPC网络通信
    Spring之 JDBC 异常
    Java性能之synchronized锁的优化
    浅谈Java中switch分支语句
    Spring Boot 之异步执行方法
    Java性能 -- Lock优化
  • 原文地址:https://www.cnblogs.com/TheQi/p/11393563.html
Copyright © 2011-2022 走看看