<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Json多層級動態結構數據解析

      一、工具

      (1)GSON
      Google Gson是一個簡單的基于Java的庫,用于將Java對象序列化為JSON,反之亦然。 它是由Google開發的一個開源庫。
      以下幾點說明為什么應該使用這個庫 -
      •  標準化 - Gson是一個由Google管理的標準化庫。
      •  高效 - 這是對Java標準庫的可靠,快速和高效的擴展。
      •  優化 - Gson庫經過高度優化。
      •  支持泛型 - 它為泛型提供了廣泛的支持。
      •  支持復雜的內部類 - 它支持具有深度繼承層次結構的復雜對象。
      (2)OGNL表達式
      OGNL表達式是Object-Graph Navigation Language的縮寫,是一種功能強大的表達式語言,通過
      簡單一致的表達式語法,可以存取對象的任意屬性,調用對象的方法,遍歷整個對象的結構圖,實現字段類型轉換。
      二、mavan所需引入的依賴
      <dependency> 
        <groupId>ognl</groupId> 
        <artifactId>ognl</artifactId> 
        <version>3.1.1</version> 
      </dependency> 
      <dependency> 
        <groupId>com.google.code.gson</groupId> 
      <artifactId>gson</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency>

      三、工具類

      public class JsonUtils { 

      /** * 將指定JSON轉為Map對象,Key固定為String,對應JSONkey * Value分情況: * 1. Value是字符串,自動轉為字符串,例如:{"a","b"} 6 * 2. Value是其他JSON對象,自動轉為Map,例如:{"a":{"b":"2"}}} * 3. Value是數組,自動轉為List<Map>,例如:{"a":[{"b":"2"},"c":"3"]} * @param json 輸入的JSON對象 * @return 動態的Map集合 */ public static Map<String,Object> transferToMap(String json) { Gson gson = new Gson(); Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType()); return map; } /** * 簡化方法 * @param json 原始的JSON數據 * @param path OGNL規則表達式 * @param clazz Value對應的目標類 * @return clazz對應數據 */ public static <T> T getValue(String json, String path, Class<T> clazz) { try { Map map = transferToMap(json); OgnlContext context = new OgnlContext(); context.setRoot(map); T value = (T) Ognl.getValue(path, context, context.getRoot()); return value; } catch (Exception e) { throw new RuntimeException(e); } } public static <T> T getValueFromMap(Map map, String path, Class<T> clazz) { try { OgnlContext context = new OgnlContext(); context.setRoot(map); T value = (T) Ognl.getValue(path, context, context.getRoot()); return value; } catch (Exception e) { throw new RuntimeException(e); } } }

      四、測試用例

      import com.itlaoqi.JsonUtils;
      import org.junit.Test;
      import java.util.List;
      import java.util.Map;
      public class JsonCase {
      /**
      * {
      * "a": {
      * "b": {
      * "c": {
      * "d": {
      * "e": "nothing"
      * }
      * }
      * }
      * }
      * }
      */
      /**
      * 超多層級JSON嵌套的快速提取
      */
      @Test
      public void case0(){
      String text = "{\n" +
      " \"a\": {\n" +
      " \"b\": {\n" +
      " \"c\": {\n" +
      " \"d\": {\n" +
      " \"e\": \"nothing\"\n" +
      " }\n" +
      " }\n" +
      " }\n" +
      " }\n" +
      "}";
      Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
      String e = JsonUtils.getValue(text, "a.b.c.d.e", String.class);
      System.out.println(e);
      }

      /**
      * {
      * "showapi_res_error": "",
      * "showapi_res_id": "628cc9850de3769f06edbb49",
      * "showapi_res_code": 0,
      * "showapi_fee_num": 1,
      * "showapi_res_body": {"ret_code":0,"area":"南 安","areaid":"101230506","areaCode":"350583","hourList":
      [{"weather_code":"07","time":"202205242000","area":"南安","wind_direction":"東南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
      {"weather_code":"07","time":"202205242100","area":"南安","wind_direction":"東 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
      {"weather_code":"07","time":"202205242200","area":"南安","wind_direction":"東 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
      {"weather_code":"07","time":"202205242300","area":"南安","wind_direction":"東 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
      {"weather_code":"07","time":"202205250000","area":"南安","wind_direction":"南 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
      {"weather_code":"07","time":"202205250100","area":"南安","wind_direction":"西 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
      {"weather_code":"07","time":"202205250200","area":"南安","wind_direction":"西北
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
      {"weather_code":"02","time":"202205250300","area":"南安","wind_direction":"西北
      風","wind_power":"0-3級 微風
      <5.4m/s","weather":"陰","areaid":"101230506","temperature":"21"},
      {"weather_code":"02","time":"202205250400","area":"南安","wind_direction":"西北
      風","wind_power":"0-3級 微風
      <5.4m/s","weather":"陰","areaid":"101230506","temperature":"21"},
      {"weather_code":"02","time":"202205250500","area":"南安","wind_direction":"西 風","wind_power":"0-3級 微風
      <5.4m/s","weather":"陰","areaid":"101230506","temperature":"21"},
      {"weather_code":"07","time":"202205250600","area":"南安","wind_direction":"西北
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
      {"weather_code":"07","time":"202205250700","area":"南安","wind_direction":"西北
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
      {"weather_code":"07","time":"202205250800","area":"南安","wind_direction":"西北
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
      {"weather_code":"07","time":"202205250900","area":"南安","wind_direction":"西南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"22"},
      {"weather_code":"07","time":"202205251000","area":"南安","wind_direction":"南 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"},
      {"weather_code":"07","time":"202205251100","area":"南安","wind_direction":"東 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"24"},
      {"weather_code":"07","time":"202205251200","area":"南安","wind_direction":"東 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"24"},
      {"weather_code":"07","time":"202205251300","area":"南安","wind_direction":"東 風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"25"},
      {"weather_code":"07","time":"202205251400","area":"南安","wind_direction":"東南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"25"},
      {"weather_code":"07","time":"202205251500","area":"南安","wind_direction":"東南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"25"},
      {"weather_code":"07","time":"202205251600","area":"南安","wind_direction":"東南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"24"},
      {"weather_code":"07","time":"202205251700","area":"南安","wind_direction":"東南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"},
      {"weather_code":"07","time":"202205251800","area":"南安","wind_direction":"東南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"},
      {"weather_code":"07","time":"202205251900","area":"南安","wind_direction":"東南
      風","wind_power":"0-3級 微風 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"}]}
      * }
      */
      private String json = "{\n" +
      "\"showapi_res_error\": \"\",\n" +
      "\"showapi_res_id\": \"628cc9850de3769f06edbb49\",\n" +
      "\"showapi_res_code\": 0,\n" +
      "\"showapi_fee_num\": 1,\n" +
      "\"showapi_res_body\": {\"ret_code\":0,\"area\":\"南安
      \",\"areaid\":\"101230506\",\"areaCode\":\"350583\",\"hourList\":
      [{\"weather_code\":\"07\",\"time\":\"202205242000\",\"area\":\"南安
      \",\"wind_direction\":\"東南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"20\"},
      {\"weather_code\":\"07\",\"time\":\"202205242100\",\"area\":\"南安
      \",\"wind_direction\":\"東風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"20\"},
      {\"weather_code\":\"07\",\"time\":\"202205242200\",\"area\":\"南安
      \",\"wind_direction\":\"東風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"20\"},
      {\"weather_code\":\"07\",\"time\":\"202205242300\",\"area\":\"南安
      \",\"wind_direction\":\"東風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"20\"},
      {\"weather_code\":\"07\",\"time\":\"202205250000\",\"area\":\"南安
      \",\"wind_direction\":\"南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"07\",\"time\":\"202205250100\",\"area\":\"南安
      \",\"wind_direction\":\"西風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"07\",\"time\":\"202205250200\",\"area\":\"南安
      \",\"wind_direction\":\"西北風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"02\",\"time\":\"202205250300\",\"area\":\"南安
      \",\"wind_direction\":\"西北風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"陰
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"02\",\"time\":\"202205250400\",\"area\":\"南安
      \",\"wind_direction\":\"西北風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"陰
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"02\",\"time\":\"202205250500\",\"area\":\"南安
      \",\"wind_direction\":\"西風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"陰
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"07\",\"time\":\"202205250600\",\"area\":\"南安
      \",\"wind_direction\":\"西北風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"07\",\"time\":\"202205250700\",\"area\":\"南安
      \",\"wind_direction\":\"西北風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"07\",\"time\":\"202205250800\",\"area\":\"南安
      \",\"wind_direction\":\"西北風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"21\"},
      {\"weather_code\":\"07\",\"time\":\"202205250900\",\"area\":\"南安
      \",\"wind_direction\":\"西南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"22\"},
      {\"weather_code\":\"07\",\"time\":\"202205251000\",\"area\":\"南安
      \",\"wind_direction\":\"南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"23\"},
      {\"weather_code\":\"07\",\"time\":\"202205251100\",\"area\":\"南安
      \",\"wind_direction\":\"東風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"24\"},
      {\"weather_code\":\"07\",\"time\":\"202205251200\",\"area\":\"南安
      \",\"wind_direction\":\"東風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"24\"},
      {\"weather_code\":\"07\",\"time\":\"202205251300\",\"area\":\"南安
      \",\"wind_direction\":\"東風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"25\"},
      {\"weather_code\":\"07\",\"time\":\"202205251400\",\"area\":\"南安
      \",\"wind_direction\":\"東南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"25\"},
      {\"weather_code\":\"07\",\"time\":\"202205251500\",\"area\":\"南安
      \",\"wind_direction\":\"東南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"25\"},
      {\"weather_code\":\"07\",\"time\":\"202205251600\",\"area\":\"南安
      \",\"wind_direction\":\"東南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"24\"},
      {\"weather_code\":\"07\",\"time\":\"202205251700\",\"area\":\"南安
      \",\"wind_direction\":\"東南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"23\"},
      {\"weather_code\":\"07\",\"time\":\"202205251800\",\"area\":\"南安
      \",\"wind_direction\":\"東南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"23\"},
      {\"weather_code\":\"07\",\"time\":\"202205251900\",\"area\":\"南安
      \",\"wind_direction\":\"東南風\",\"wind_power\":\"0-3級 微風 <5.4m/s\",\"weather\":\"小雨
      \",\"areaid\":\"101230506\",\"temperature\":\"23\"}]}\n" +
      "}";

      //將JSON轉為標準Map結構
      @Test
      public void case1(){
      Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
      System.out.println(jsonMap);
      }
      /**
      * OGNL直接提取數據,Value為子JSON對象的情況
      */
      @Test
      public void case2(){
      Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
      Map resBody = JsonUtils.getValue(json, "showapi_res_body", Map.class);
      System.out.println(resBody);
      }

      /**
      * OGNL直接提取數據,Value為標準字符串的情況
      */
      @Test
      public void case3(){
      Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
      String value = JsonUtils.getValue(json, "showapi_res_body.area", String.class);
      System.out.println(value);
      }

      /**
      * OGNL直接提取數據,Value為標準字符串的情況,Value為數組的情況
      */
      @Test
      public void case4(){
      Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
      List<Map> hourList = JsonUtils.getValue(json, "showapi_res_body.hourList",st.class);
      System.out.println(hourList);
      // 每一個集合對象都是List
      for(Map hour : hourList){
      System.out.println(hour);
      }
      }

      /**
      * 利用List語法獲取第6個時點天氣預報
      */
      @Test
      public void case5(){
      Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
      String area = JsonUtils.getValue(json,"showapi_res_body.hourList[5].weather_code", String.class);
      System.out.println(area);
      }
      }

       

      posted @ 2022-05-30 09:32  [奮斗]  閱讀(2082)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 久久av无码精品人妻系列试探| 狠狠躁夜夜人人爽天96| 高清无打码一区二区三区| 欧洲亚洲国内老熟女超碰| 国产午夜福利免费入口| 国产精品老熟女免费视频| 五月天免费中文字幕av| 麻豆国产高清精品国在线| 麻豆国产尤物av尤物在线观看 | 狠狠综合久久久久综| 深夜福利资源在线观看| 丝袜老师办公室里做好紧好爽| 99福利一区二区视频| 又污又爽又黄的网站| 久久99日韩国产精品久久99| 两个人的视频www免费| 香蕉亚洲欧洲在线一区| 亚洲高清国产自产拍av| 亚洲国产成人精品无码区蜜柚| 国产综合视频一区二区三区| 四虎影视www在线播放| 91精品国产91热久久久久福利 | 高清美女视频一区二区三区| 国产成人精品亚洲资源| 国产精品XXXX国产喷水| 91精品91久久久久久| 国产日韩久久免费影院| 国产精品国产精品国产精品| 五月综合激情婷婷六月| 无码国内精品久久人妻蜜桃| 在线a亚洲老鸭窝天堂| 人妻日韩人妻中文字幕| 97久久超碰精品视觉盛宴| 亚洲精品成人福利网站| 天堂…中文在线最新版在线| 亚洲欧美人成人综合在线播放| 免费午夜无码片在线观看影院| 91国产自拍一区二区三区| 国产91小视频在线观看| 国产一区二区黄色在线观看| 狠狠爱五月丁香亚洲综|