猿问

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

给定一个 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


以下不适合处理数组时的解决方案,例如“评论”,然后以编程方式加入:


   List ids = JsonPath.read(json, "$.store.books[*].book_id");

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


达令说
浏览 297回答 1
1回答

Qyouu

按照 上的文档https://github.com/json-path/JsonPath,这应该给你你正在寻找的东西。List output = JsonPath.read(json, "$.books[*].['book_id', 'reviews'])");看看这个测试用例。@Testfinal 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());}   
随时随地看视频慕课网APP

相关分类

Java
我要回答