JAVA——jsonpath使用

记录下几种常用用法:

添加依赖:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

测试如下:

/**
 * TestJsonPath 各种类型测试
 */
@Test
public void TestJsonPath() {

    String JsonString =  "{\n" +
            "    \"store\": {\n" +
            "        \"book\": [\n" +
            "            {\n" +
            "                \"category\": \"reference\",\n" +
            "                \"author\": \"Nigel Rees\",\n" +
            "                \"title\": \"Sayings of the Century\",\n" +
            "                \"price\": 8.95\n" +
            "            },\n" +
            "            {\n" +
            "                \"category\": \"fiction\",\n" +
            "                \"author\": \"Evelyn Waugh\",\n" +
            "                \"title\": \"Sword of Honour\",\n" +
            "                \"price\": 12.99\n" +
            "            },\n" +
            "            {\n" +
            "                \"category\": \"fiction\",\n" +
            "                \"author\": \"Herman Melville\",\n" +
            "                \"title\": \"Moby Dick\",\n" +
            "                \"isbn\": \"0-553-21311-3\",\n" +
            "                \"price\": 8.99\n" +
            "            },\n" +
            "            {\n" +
            "                \"category\": \"fiction\",\n" +
            "                \"author\": \"J. R. R. Tolkien\",\n" +
            "                \"title\": \"The Lord of the Rings\",\n" +
            "                \"isbn\": \"0-395-19395-8\",\n" +
            "                \"price\": 22.99\n" +
            "            }\n" +
            "        ],\n" +
            "        \"bicycle\": {\n" +
            "            \"color\": \"red\",\n" +
            "            \"price\": 19.95\n" +
            "        }\n" +
            "    },\n" +
            "    \"expensive\": \"10\"\n" +
            "}" ;

    // 输出 所有 book 对象
    String jsonPath = "$.store.book.[*]";
    List msg = JsonPath.read(JsonString, jsonPath);
    System.out.println(msg.toString());
    //[{"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}]

    //输出 所有 author 字段
    jsonPath = "$.store.book[*].author";
    List readMsg = JsonPath.read(JsonString, jsonPath);
    System.out.println(readMsg.toString());
    //["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]

    //输出的 集合不存在 不会空指针 List = []
    jsonPath = "$.store.book[*].author111";
    List readMsg1 = JsonPath.read(JsonString, jsonPath);
    System.out.println(readMsg1.toString());
    // []

    // 输出 单个字段 接受需要指定类型
    jsonPath = "$.expensive";
    String readMsg2 = JsonPath.read(JsonString, jsonPath);
    System.out.println(readMsg2);
    // 10

    // 取出list 用LinkedHashMap 接收
    String jsonPath4 = "$.store.book";
    List<LinkedHashMap> msg4 = JsonPath.read(JsonString, jsonPath4);
    System.out.println(msg4.toString());

    // List<LinkedHashMap> 转换 jsonArray
    JSONArray myResult = new JSONArray();
    for (LinkedHashMap linkedHashMap :msg4) {
        JSONObject jSONObject = new JSONObject();
        Iterator it = linkedHashMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            jSONObject.put((String) entry.getKey(), entry.getValue());
        }
        myResult.put(jSONObject);
    }
    System.out.println(myResult);


    //修改 某个字段值
    DocumentContext doc = JsonPath.parse(JsonString).set("$.store.book.[2].author", "Jayway");
    System.out.println(doc.read("$.store.book.[1].author").toString());
    System.out.println(doc.read("$.store.book.[2].author").toString());

    //修改全部
    doc = JsonPath.parse(JsonString).set("$.store.book.[*].author", "Jayway");
    System.out.println(doc.read("$.store.book.[*].author").toString());

    //DocumentContext 转string
    String docString = doc.jsonString();
    System.out.println(docString);

}

官方:

https://github.com/json-path/JsonPath

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值