为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

达令说 2022-01-06 17:30:10
给定一个 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[*]");
查看完整描述

1 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

按照 上的文档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());


}   



查看完整回答
反对 回复 2022-01-06
  • 1 回答
  • 0 关注
  • 296 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号