java - 如何使用像 Jayway JsonPath 这样的 java 库添加新的 json 节点
问题描述
我有一个 json 字符串说:
{"store" : {
"book" : [
{
"category" : "reference",
"author" : "Nigel Rees",
"title" : "Sayings of the Century",
"display-price" : 8.95
},
{
"category" : "fiction",
"author" : "Evelyn Waugh",
"title" : "Sword of Honour",
"display-price" : 12.99
},
{
"category" : "fiction",
"author" : "Herman Melville",
"title" : "Moby Dick",
"isbn" : "0-553-21311-3",
"display-price" : 8.99
},
{
"category" : "fiction",
"author" : "J. R. R. Tolkien",
"title" : "The Lord of the Rings",
"isbn" : "0-395-19395-8",
"display-price" : 22.99
}
]
}
我想根据某些条件在这个字符串中添加一个新的 json 节点
example: add $.book[0].origin = 'burma' if not present
我已经搜索了一个可以做到这一点但找不到的图书馆。我试过JsonPath。
有没有人使用过任何可以满足这个用例的库?
解决方案
您可以使用 Jackson 库实现您想要的。首先获取rootNode,然后获取子节点并获取给定索引处的元素。然后使用 addPojo 方法创建新的 Book 对象并将其附加到 book 数组。
这是代码:
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("test.json");
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(file);
JsonNode store = rootNode.get("store");
JsonNode books = store.get("book");
// get an element in that node
JsonNode bookElement = books.get(0);
Book book = new Book();
book.setAuthor("test");
book.setCategory("test");
book.setDisplayPrice("test");
book.setTitle("test");
// you can change the logic with editing following lines
// if has the desired field
if (bookElement.hasNonNull("origin")) {
// if field value is not equal to given text
if (!bookElement.get("origin").textValue().equalsIgnoreCase("burma")) {
((ArrayNode)books).addPOJO(book);
}
}
// write to file
mapper.writeValue(file, rootNode);
}
}
书类:
@Data // comes from lombok for getter-setter
class Book {
private String category;
private String author;
private String title;
@JsonProperty("display-price")
private String displayPrice;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String isbn;
}
如果您有字段来源并且它不等于“缅甸”,则文件在附加对象后变为:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"display-price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"display-price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"display-price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"display-price": 22.99
},
{
"category": "test",
"author": "test",
"title": "test",
"display-price": "test"
}
]
}
}
杰克逊专家:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
推荐阅读
- android - 错误:最近更新 android 插件 3.2.1、gradle 4.6 后,prolog 中不允许内容,drawable 中的 colors.xml 和 xml 文件中的错误
- node.js - 电子:权限被拒绝代码 ELIFECYCLE errno 126
- macos - 安装 go 程序的问题
- sql - 我可以在 PHPMyadmin 上放置一个 SQL 查询来自动实现吗?
- arrays - 在多个文本文件中搜索相似数据
- ios - iOS MapKit:在标注中点击触摸在标注下呈现的annotationView
- html - 页面刷新后,我该怎么做才能保持在同一个子选项卡上?
- r - 清理 PDF 文件中的数据
- mysql - MySQL Query 两次返回同一行
- angular - 如何从 ngFor 创建的项目生成不同的 routerLinks?