代码之家  ›  专栏  ›  技术社区  ›  atlas_scoffed

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

  •  0
  • atlas_scoffed  · 技术社区  · 6 年前

    给定一个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": "...",
        "reviews": [
            ...
        ]
        },
        {
            ...
        }
    ]
    

    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 回复  |  直到 6 年前
        1
  •  4
  •   Raj    6 年前

    在下面的文档中 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());
    
    }