JsonPath 设置值

在 JsonPath 中,我们可以通过 set() 方法对 JSON 进行设置,例如:

// 设置第一本书籍的作者为 Paul
String newJson = JsonPath.parse(json).set("$['store']['book'][0]['author']", "Paul").jsonString();

我们来看一个完整的示例:

package com.hxstrive.json_path;

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

import java.util.List;

/**
 * Jayway JsonPath 示例
 * @author hxstrive.com
 */
public class Demo13 {

    public static void main(String[] args) {
        String json = "{" +
                "    \"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" +
                "        }" +
                "    }," +
                "    \"expensive\": 10" +
                "}";

        String color = JsonPath.parse(json).read("$.store.bicycle.color");
        System.out.println("color=" + color);

        String newJson = JsonPath.parse(json).set("$.store.bicycle.color", "blue").jsonString();
        color = JsonPath.parse(newJson).read("$.store.bicycle.color");
        System.out.println("color=" + color);
    }

}

// 输出结果:
//color=red
//color=blue

注意,我们还可以进行批量修改,示例如下:

package com.hxstrive.json_path;

import com.jayway.jsonpath.JsonPath;
import java.util.List;

/**
 * Jayway JsonPath 示例
 * @author hxstrive.com
 */
public class Demo14 {

    public static void main(String[] args) {
        String json = "{" +
                "    \"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" +
                "        }" +
                "    }," +
                "    \"expensive\": 10" +
                "}";

        List<String> price = JsonPath.parse(json).read("$.store.book[*].price");
        System.out.println("price=" + price);

        String newJson = JsonPath.parse(json).set("$.store.book[*].price", 99.99).jsonString();
        price = JsonPath.parse(newJson).read("$.store.book[*].price");
        System.out.println("price=" + price);
    }

}

// 输出结果:
//price=[8.95,12.99,8.99,22.99]
//price=[99.99,99.99,99.99,99.99]
说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号