使用JSON Path从父对象和子对象中提取特定字段/数组

4

给定一个类似以下JSON对象:

"book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "book_id": "214515",
                "title": "Sayings of the Century",
                "price": 8.95,
                "reviews": [
                {
                  "rating": 2,
                  "reviewer": "Amazon",
                  "weight": 0
                },
                {
                ...
                }
            ]
        },
        {
        ...
        }

是否有可能选择书籍的book_id,以及该书籍的部分或全部评论?

结果可能类似于下面这样:

[
    {
    "book_id": "...",
    "reviews": [
        ...
    ]
    },
    {
        ...
    }
]

我一直在使用Jayway jsonpath:https://github.com/json-path/JsonPath

当处理数组(如“reviews”)并以编程方式进行连接时,以下内容不适用于解决方案:

   List ids = JsonPath.read(json, "$.store.books[*].book_id");
   List reviews =  JsonPath.read(json, "$.store.books[*].reviews[*]");
1个回答

8

按照https://github.com/json-path/JsonPath上的文档,这应该可以给你想要的结果。

List output = JsonPath.read(json, "$.books[*].['book_id', 'reviews'])");

请看这个测试用例。

@Test
final void test2() {
    String json = "{ \"books\": [" 
            + "                    {" 
            + "                        \"category\": \"reference\","
            + "                        \"author\": \"Nigel Rees\","
            + "                        \"book_id\": \"214515\","
            + "                        \"title\": \"Sayings of the Century\","
            + "                        \"price\": 8.95," 
            + "                        \"reviews\": ["
            + "                        {" 
            + "                          \"rating\": 2,"
            + "                          \"reviewer\": \"Amazon\"," 
            + "                          \"weight\": 0"
            + "                        }" 
            + "                      ]" 
            + "                    },"
            + "                    {" 
            + "                        \"category\": \"reference\","
            + "                        \"author\": \"Nigel Rees\","
            + "                        \"book_id\": \"314515\","
            + "                        \"title\": \"Sayings of the Century\","
            + "                        \"price\": 8.95," 
            + "                        \"reviews\": ["
            + "                        {" 
            + "                          \"rating\": 4,"
            + "                          \"reviewer\": \"Trip\"," 
            + "                          \"weight\": 5"
            + "                        }" 
            + "                      ]" 
            + "                    }"

            + "               ]" 
            + "}";

    List output = JsonPath.read(json, "$.books[*].['book_id', 'reviews'])");

    String expectedOutput = 
            "["
            + "{"
                + "\"book_id\":\"214515\","
                + "\"reviews\":["
                                + "{"
                                        + "\"rating\":2,\"reviewer\":\"Amazon\",\"weight\":0"
                                + "}"
                            + "]"
            + "},"
            + "{"
                + "\"book_id\":\"314515\","
                + "\"reviews\":["
                                + "{\"rating\":4,\"reviewer\":\"Trip\",\"weight\":5}"
                            + "]"
            + "}"
        + "]";

    assertEquals(expectedOutput, output.toString());

}   

哈哈!就是这样。谢谢Raj。我完全错过了那一部分。你是从文档中的“括号标记的子元素或子元素”那里得到的吗? - atlas_scoffed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接