第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JSON Path 使用 JSON Path 從父對象和子對象中提取特定字段/數(shù)組

JSON Path 使用 JSON Path 從父對象和子對象中提取特定字段/數(shù)組

達令說 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 以及該書的部分或全部評論?結(jié)果可能如下所示:[    {    "book_id": "...",    "reviews": [        ...    ]    },    {        ...    }]我一直在使用 Jayway jsonpath:https : //github.com/json-path/JsonPath以下不適合處理數(shù)組時的解決方案,例如“評論”,然后以編程方式加入:   List ids = JsonPath.read(json, "$.store.books[*].book_id");   List reviews =  JsonPath.read(json, "$.store.books[*].reviews[*]");
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經(jīng)驗 獲得超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 關(guān)注
  • 333 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號