在 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]