java - 如何使用 Jayway JsonPath 等 Java 库添加新的 json 节点

标签 java jsonpath json-path-expression jayway

我有一个 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,然后获取子节点并获取给定索引处的元素。然后创建新的 Book 对象并使用 addPojo 方法将其附加到 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;
}

如果您有字段来源并且它不等于“burma”,则文件在附加对象后将变为:

{
  "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"
      }
    ]
  }
}

jackson 专家:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
    </dependency>

关于java - 如何使用 Jayway JsonPath 等 Java 库添加新的 json 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52014952/

相关文章:

java - 使用对象数组/对象数组求平均值

java - 如何使用 MockMvcResultMatchers.jsonPath

javascript - 使用 jsonpath 提取 json 中的元素值

java - 使用构造函数查找孪生素数

java - 将生成的xml存储到java中的字符串变量中

java - 套接字发送和接收字节数组

php - 制作一个 JSONpath 表达式以便它只检索一个特定的值?

java - Jayway JsonPath读取长Java

Json 路径 - 对列表列表进行过滤