最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于json解析多層嵌套并轉(zhuǎn)為對應(yīng)類(List)

 更新時間:2024年10月15日 14:41:42   作者:世間所有相遇都是久別重逢  
在進行JSON解析時,遇到多層嵌套結(jié)構(gòu)可通過遞歸或?qū)S脦靵韺崿F(xiàn),重要的是將嵌套的JSON對象準確轉(zhuǎn)化為對應(yīng)的Java類,通常需要依賴如Gson或Jackson等庫,將JSONObject轉(zhuǎn)為JavaBean時,關(guān)注字段匹配與數(shù)據(jù)類型轉(zhuǎn)換

json解析多層嵌套并轉(zhuǎn)為對應(yīng)類(List)

Json(隨便扒的格式,將就看~)

{
    "code": 1,
    "message": "查詢成功",
    "data": [
        {
            "type": 1,
            "question": "地層壓力與同井深的淡水靜液壓力之比稱為地層的()。",
            "answer": "1",
            "id": 1,
            "description": "題目描述",
            "answers": [
                {
                    "isCorrect": "1",
                    "answer_name": "A的選項內(nèi)容"
                },
                {
                    "isCorrect": "0",
                    "answer_name": "B的選項內(nèi)容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "C的選項內(nèi)容"
                },
                {
                    "isCorect": "0",
                    "answer_name": "D的選項內(nèi)容"
                }
            ]
        },
        {
            "type": 1,
            "question": "起鉆時,產(chǎn)生的抽吸壓力導(dǎo)致井底壓力()。",
            "answer": "1",
            "id": 1,
            "description": "題目描述",
            "answers": [
                {
                    "isCorrect": 1,
                    "answer_name": "A的選項內(nèi)容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "B的選項內(nèi)容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "C的選項內(nèi)容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "D的選項內(nèi)容"
                }
            ]
        }
    ]
}

關(guān)鍵依賴

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.72</version>
        </dependency>
JSONObject param = testDate();
        JSONObject param1 = testHeader("QUERY");
        String rDate = HealthServerHandler.getInstance().post(url,param1.toJSONString(), param.toJSONString());
        //這個依賴是com.google.code.gson;因為報錯原因未知(報錯下面圖中顯示),更換了阿里的依賴
        //RecOrder<ResultValue<PurchaseOrder, PageInfo>> recOrder = new Gson().fromJson(rDate, RecOrder.class);
        //處理json將類填充,入庫
        JSONObject rDateJSON = JSONObject.parseObject(rDate);
        //這里獲取的是"resultValue"標(biāo)簽下面“data”這個JSONArray
        JSONArray data = rDateJSON.getJSONObject("resultValue").getJSONArray("data");
        //這里獲取的是"resultValue"標(biāo)簽下面“pageInfo”這個JSONObject
        JSONObject pageInfo = rDateJSON.getJSONObject("resultValue").getJSONObject("pageInfo");
        //PurchaseOrder 是具體接收類
        List<PurchaseOrder> purchaseOrder = JSONArray.parseArray(data.toString(),PurchaseOrder.class);
        PageInfo pageInfo1 = JSONObject.parseObject(pageInfo.toString(),PageInfo.class);
        //茵PageInfo與PurchaseOrder并不在同一結(jié)構(gòu)內(nèi),需要二次添加
        for (PurchaseOrder order : purchaseOrder) {
            order.setPageCount(pageInfo1.getPageCount());
            order.setPageNum(pageInfo1.getPageNum());
            order.setTotal(pageInfo1.getTotal());
            order.setPageSize(pageInfo1.getPageSize());
            service.saveOrder(order);
        }

//具體解析  大佬原作網(wǎng)址https://www.cnblogs.com/janson071/p/9646678.html
package jansonDemo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class TestJSON {
    /**
     * JSON實際上也是鍵值對("key":"value")
     * key 必須是字符串,value 可以是合法的 JSON 數(shù)據(jù)類型(字符串, 數(shù)字, 對象, 數(shù)組, 布爾值或 null)
     * value如果是字符串,用jsonobj.getString("key")獲取
     * value如果是數(shù)  字,用jsonobj.getIntValue("key"),jsonobj.getFloatValue("key"),jsonobj.getInteger("key")等基本數(shù)據(jù)類型及其包裝類的方法獲取
     * value如果是布爾值,用jsonobj.getBoolean("key"),jsonobj.getBooleanValue("key")獲取
     * value如果是數(shù)  組,用jsonobj.getJSONArray("key")獲取
     * value如果是Object對象,用jsonobj.get("key"),獲取
     * value如果是JSONObject對象,用jsonobj.getJSONObject("key")獲取
     */

    /**
     * 該方法用于將已有的json字符串轉(zhuǎn)換為json對象,并取出該對象中相應(yīng)的key對應(yīng)的value值
     * 將已有的字符串轉(zhuǎn)換成jsonobject,用JSON.parseObject(jsonStr)方法
     * json中只要是{}就代表一個JSONObject,[]就代表一個JSONArray
     * 獲取JSONObject對象用JSONObject jsonobject.getJSONObject("key")方法
     * 獲取JSONArray對象用JSONObject jsonobject.getJSONArray("key")方法
     */

    private static void strWritedToJSONObject() {
        //以下是一個json對象中嵌套一個json子對象
        String myJsonObj = "{\n" +
                "    \"name\":\"runoob\",\n" +
                "    \"alexa\":10000,\n" +
                "    \"sites\": {\n" +
                "        \"site1\":\"www.runoob.com\",\n" +
                "        \"site2\":\"m.runoob.com\",\n" +
                "        \"site3\":\"c.runoob.com\"\n" +
                "    }\n" +
                "}";
        JSONObject jsonobj = JSON.parseObject(myJsonObj); //將json字符串轉(zhuǎn)換成jsonObject對象
        /***獲取JSONObject中每個key對應(yīng)的value值時,可以根據(jù)實際場景中想得到什么類型就分別運用不到的方法***/
        System.out.println(jsonobj.get("name")); //取出name對應(yīng)的value值,得到的是一個object
        System.out.println(jsonobj.getString("name")); //取出name對應(yīng)的value值,得到的是一個String
        System.out.println(jsonobj.getIntValue("alexa")); //取出name對應(yīng)的value值,得到的是一個int
        System.out.println(jsonobj.get("sites")); //取出sites對應(yīng)的value值,得到的是一個object
        System.out.println(jsonobj.getString("sites"));
        System.out.println(jsonobj.getJSONObject("sites")); //取出sites對應(yīng)的value值,得到一個JSONObject子對象
        System.out.println(jsonobj.getJSONObject("sites").getString("site2")); //取出嵌套的JSONObject子對象中site2對應(yīng)的value值,必須用getJSONObject()先獲取JSONObject


        /**
         * 以下是一個json對象中包含數(shù)組,數(shù)組中又包含json子對象和子數(shù)組
         */
        String myJsonObj2 = "{\n" +
                "    \"name\":\"網(wǎng)站\",\n" +
                "    \"num\":3,\n" +
                "    \"sites\": [\n" +
                "        { \"name\":\"Google\", \"info\":[ \"Android\", \"Google 搜索\", \"Google 翻譯\" ] },\n" +
                "        { \"name\":\"Runoob\", \"info\":[ \"菜鳥教程\", \"菜鳥工具\", \"菜鳥微信\" ] },\n" +
                "        { \"name\":\"Taobao\", \"info\":[ \"淘寶\", \"網(wǎng)購\" ] }\n" +
                "    ]\n" +
                "}";
        JSONObject jsonobj2 = JSON.parseObject(myJsonObj2); //將json字符串轉(zhuǎn)換成jsonObject對象
        System.out.println(jsonobj2.get("sites"));
        System.out.println(jsonobj2.getString("sites"));
        System.out.println(jsonobj2.getJSONArray("sites")); //取出sites對應(yīng)的value值,得到一個JSONOArray對象
        //System.out.println(jsonobj2.getJSONObject("sites")); 不能用該方法,因為sites是一個JSONOArray對象
        //取出json對象中sites對應(yīng)數(shù)組中第一個json子對象的值
        System.out.println(jsonobj2.getJSONArray("sites").getJSONObject(0)); //得到結(jié)果:{"name":"Google","info":["Android","Google 搜索","Google 翻譯"]}
        System.out.println(jsonobj2.getJSONArray("sites").get(0));
        System.out.println(jsonobj2.getJSONArray("sites").getString(0));
        //取出json對象中sites對應(yīng)數(shù)組中第一個json子對象下面info對應(yīng)的嵌套子數(shù)組值
        System.out.println(jsonobj2.getJSONArray("sites").getJSONObject(0).getJSONArray("info")); //得到結(jié)果:["Android","Google 搜索","Google 翻譯"]
        //取出json對象中sites對應(yīng)數(shù)組中第一個json子對象下面info對應(yīng)的嵌套子數(shù)組中第二個值
        System.out.println(jsonobj2.getJSONArray("sites").getJSONObject(0).getJSONArray("info").getString(1)); //得到結(jié)果:Google 搜索

        //依次取出json對象中sites對應(yīng)數(shù)組中的值
        JSONArray array = jsonobj2.getJSONArray("sites");
        getJsonArrayItem(array);
        //依次取出json對象中sites對應(yīng)數(shù)組中第二個json子對象下面info對應(yīng)的嵌套子數(shù)組值
        JSONArray arr = jsonobj2.getJSONArray("sites").getJSONObject(1).getJSONArray("info");
        getJsonArrayItem(arr);
     }

    /**
     * 手動添加對象到一個JSONObject
     */
    private static void writeStrToJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","tom");
        jsonObject.put("age",20);

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonArrayObject1 = new JSONObject();
        jsonArrayObject1.put("name","alibaba");
        jsonArrayObject1.put("info","www.alibaba.com");
        JSONObject jsonArrayObject2 = new JSONObject();
        jsonArrayObject2.put("name","baidu");
        jsonArrayObject2.put("info","www.baidu.com");

        jsonArray.add(jsonArrayObject1);
        jsonArray.add(jsonArrayObject2);

        jsonObject.put("sites",jsonArray);

        System.out.println(jsonObject);
     }

    /**
     * 將字符串轉(zhuǎn)為JSONArray
     */
    private static void strToJsonArray() {
        String arrayStr = "[\n" +
                "        {\n" +
                "            \"name\":\"alibaba\",\n" +
                "            \"info\":\"www.alibaba.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\":\"baidu\",\n" +
                "            \"info\":\"www.baidu.com\"\n" +
                "        }\n" +
                "    ]";

        JSONArray array = JSON.parseArray(arrayStr);
        System.out.println(array);
     }

    /**
     * 依次取出JSONArray中的值
     */
    private static void getJsonArrayItem(JSONArray array) {
        for (int i=0; i<array.size(); i++) {
            System.out.println(array.get(i));
        }
    }

     //測試類
    public static void main(String[] args) {
        strWritedToJSONObject();
        //writeStrToJSONObject();
        //strToJsonArray();
    }
}

JAVABEAN轉(zhuǎn)JSONObject

String jsonString=JSONObject.toJSONString(param);
JSONObject jSONObject=JSONObject.parseObject(jsonString);

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 簡單講解Android開發(fā)中觸摸和點擊事件的相關(guān)編程方法

    簡單講解Android開發(fā)中觸摸和點擊事件的相關(guān)編程方法

    這篇文章主要介紹了Android開發(fā)中觸摸和點擊事件的相關(guān)編程方法,包括事件偵聽器等安卓開發(fā)中常用的接口的基本使用方法,需要的朋友可以參考下
    2015-12-12
  • JAVA多線程之實現(xiàn)用戶任務(wù)排隊并預(yù)估排隊時長

    JAVA多線程之實現(xiàn)用戶任務(wù)排隊并預(yù)估排隊時長

    本文主要介紹了Java多線程之實現(xiàn)用戶任務(wù)排隊并預(yù)估排隊時長的問題,文中的代碼具有一定的學(xué)習(xí)和工作價值,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧
    2021-12-12
  • Java并發(fā)編程之LongAdder執(zhí)行情況解析

    Java并發(fā)編程之LongAdder執(zhí)行情況解析

    這篇文章主要為大家介紹了Java并發(fā)編程之LongAdder執(zhí)行情況解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Springboot的jdk安裝與配置教程

    Springboot的jdk安裝與配置教程

    本文介紹了在Windows、macOS和Linux系統(tǒng)上下載和配置JDK(Java Development Kit)的步驟,它涵蓋了從下載JDK、選擇合適的安裝路徑、配置環(huán)境變量到驗證安裝的全過程,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • Java中的@PostConstruct注解

    Java中的@PostConstruct注解

    本文給大家介紹Java中的@PostConstruct注解的相關(guān)知識,通過示例代碼給大家補充介紹了Java提供的@PostConstruct注解,Spring是如何實現(xiàn)的,感興趣的朋友一起看看吧
    2025-04-04
  • Java線程之join_動力節(jié)點Java學(xué)院整理

    Java線程之join_動力節(jié)點Java學(xué)院整理

    join() 定義在Thread.java中,下文通過源碼分享join(),需要的朋友參考下吧
    2017-05-05
  • Java 中的類和對象詳情

    Java 中的類和對象詳情

    這篇文章主要介紹了Java 中的類和對象,類可以看成是創(chuàng)建Java對象的模板,下面文章圍繞著Java 類與對象詳細內(nèi)容展開,需要的朋友可以參考一下
    2021-11-11
  • SpringBoot中的聲明式事務(wù)詳解

    SpringBoot中的聲明式事務(wù)詳解

    這篇文章主要介紹了SpringBoot中的聲明式事務(wù)詳解,Spring采用統(tǒng)一的機制來處理不同的數(shù)據(jù)訪問技術(shù)的事務(wù), Spring的事務(wù)提供一個PlatformTransactionManager的接口,不同的數(shù)據(jù)訪問技術(shù)使用不同的接口實現(xiàn),需要的朋友可以參考下
    2023-08-08
  • springboot前端傳參date類型后臺處理的方式

    springboot前端傳參date類型后臺處理的方式

    這篇文章主要介紹了springboot前端傳參date類型后臺處理的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 關(guān)于spring中單例Bean引用原型Bean產(chǎn)生的問題及解決

    關(guān)于spring中單例Bean引用原型Bean產(chǎn)生的問題及解決

    這篇文章主要介紹了關(guān)于spring中單例Bean引用原型Bean產(chǎn)生的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評論

固始县| 普陀区| 绩溪县| 沁水县| 蚌埠市| 彭泽县| 汤阴县| 高唐县| 日照市| 邵武市| 开平市| 卫辉市| 平阳县| 普兰店市| 诏安县| 团风县| 上虞市| 清水河县| 井陉县| 琼结县| 夏河县| 商洛市| 榕江县| 株洲县| 秦皇岛市| 和政县| 乐至县| 遂宁市| 克山县| 江安县| 库伦旗| 贵州省| 太谷县| 桂阳县| 正宁县| 丰顺县| 五常市| 神农架林区| 黄梅县| 宣威市| 焉耆|