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

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

 

posted @ 2019-08-22 11:56  TheQi  阅读(232)  评论(0编辑  收藏  举报