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

Java中利用gson解析Json實例教程

 更新時間:2017年05月24日 09:59:18   作者:jihite  
這篇文章主要給大家介紹了關(guān)于Java中利用gson解析Json 的相關(guān)資料,文中給出了詳細的示例代碼供大家參考學習,相信對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

本文主要跟大家介紹了關(guān)于Java用gson解析Json的相關(guān)內(nèi)容,分享出來供大家參考學習,需要的朋友們下面來一起看看吧。

json數(shù)據(jù)

{

 "resultcode": "200",

 "reason": "successed!",

 "result": {

  "sk": {

   "temp": "24",

   "wind_direction": "西南風",

   "wind_strength": "2級",

   "humidity": "51%",

   "time": "10:11"

  },

  "today": {

   "temperature": "16℃~27℃",

   "weather": "陰轉(zhuǎn)多云",

   "weather_id": {

    "fa": "02",

    "fb": "01"

   },

   "wind": "西南風3-4 級",

   "week": "星期四",

   "city": "濱州",

   "date_y": "2015年06月04日",

   "dressing_index": "舒適",

   "dressing_advice": "建議著長袖T恤、襯衫加單褲等服裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",

   "uv_index": "最弱",

   "comfort_index": "",

   "wash_index": "較適宜",

   "travel_index": "",

   "exercise_index": "較適宜",

   "drying_index": ""

  },

  "future": [

   {

    "temperature": "16℃~27℃",

    "weather": "陰轉(zhuǎn)多云",

    "weather_id": {

     "fa": "02",

     "fb": "01"

    },

    "wind": "西南風3-4 級",

    "week": "星期四",

    "date": "20150604"

   },

   {

    "temperature": "20℃~32℃",

    "weather": "多云轉(zhuǎn)晴",

    "weather_id": {

     "fa": "01",

     "fb": "00"

    },

    "wind": "西風3-4 級",

    "week": "星期五",

    "date": "20150605"

   },

   {

    "temperature": "23℃~35℃",

    "weather": "多云轉(zhuǎn)陰",

    "weather_id": {

     "fa": "01",

     "fb": "02"

    },

    "wind": "西南風3-4 級",

    "week": "星期六",

    "date": "20150606"

   },

   {

    "temperature": "20℃~33℃",

    "weather": "多云",

    "weather_id": {

     "fa": "01",

     "fb": "01"

    },

    "wind": "北風微風",

    "week": "星期日",

    "date": "20150607"

   },

   {

    "temperature": "22℃~34℃",

    "weather": "多云",

    "weather_id": {

     "fa": "01",

     "fb": "01"

    },

    "wind": "西南風3-4 級",

    "week": "星期一",

    "date": "20150608"

   },

   {

    "temperature": "22℃~33℃",

    "weather": "陰",

    "weather_id": {

     "fa": "02",

     "fb": "02"

    },

    "wind": "西南風3-4 級",

    "week": "星期二",

    "date": "20150609"

   },

   {

    "temperature": "22℃~33℃",

    "weather": "多云",

    "weather_id": {

     "fa": "01",

     "fb": "01"

    },

    "wind": "南風3-4 級",

    "week": "星期三",

    "date": "20150610"

   }

  ]

 },

 "error_code": 0

} 

解析JSONObject

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException;

import java.io.FileNotFoundException;
import java.io.FileReader;

public class ReadJson {
 public static void main(String []args) {
  JsonParser parse = new JsonParser();
  try {
   JsonObject json = (JsonObject) parse.parse(new FileReader("weather.json"));
   System.out.println("resultcode:" + json.get("resultcodeu").getAsInt());
   System.out.println("reason:" + json.get("reason").getAsString());
   JsonObject result = json.get("result").getAsJsonObject();
   JsonObject today = result.get("today").getAsJsonObject();
   System.out.println("weak:" + today.get("week").getAsString());
   System.out.println("weather:" + today.get("weather").getAsString());
  } catch (JsonIOException e) {
   e.printStackTrace();
  } catch (NullPointerException e) {
   e.printStackTrace();
  } catch (JsonSyntaxException e){
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
}

解析JSONArray

import com.google.gson.JsonParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException;

import java.io.FileNotFoundException;
import java.io.FileReader;

public class ReadJsonArray {
 public static void main(String []args) {
  JsonParser parse = new JsonParser();
  try {
   JsonObject json = (JsonObject)parse.parse(new FileReader("C:\\Users\\wzh94434\\IdeaProjects\\TestProject\\jsontest\\src\\main\\java\\weather.json"));
   JsonObject result = json.get("result").getAsJsonObject();
   JsonArray futureArray = result.get("future").getAsJsonArray();
   for (int i = 0; i < futureArray.size(); ++i) {
    JsonObject subObj = futureArray.get(i).getAsJsonObject();
    System.out.println("------");
    System.out.println("week:" + subObj.get("week").getAsString());
    System.out.println("weather:" + subObj.get("weather").getAsString());
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (JsonIOException e) {
   e.printStackTrace();
  } catch (JsonSyntaxException e) {
   e.printStackTrace();
  }
 }
}

注意:文件路徑相對路徑是從工程根目錄開始

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • velocity顯示List與Map的方法詳細解析

    velocity顯示List與Map的方法詳細解析

    以下是對velocity顯示List與Map的方法進行了詳細的介紹。需要的朋友可以過來參考下
    2013-08-08
  • 解讀@Bean和@Autowired、@Resource之間的區(qū)別

    解讀@Bean和@Autowired、@Resource之間的區(qū)別

    這篇文章主要介紹了@Bean和@Autowired、@Resource之間的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java生成pdf文件或jpg圖片的案例講解

    Java生成pdf文件或jpg圖片的案例講解

    這篇文章主要介紹了Java生成pdf文件或jpg圖片的案例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • java回溯算法解數(shù)獨問題

    java回溯算法解數(shù)獨問題

    這篇文章主要為大家詳細介紹了java回溯算法解數(shù)獨問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • mybatis-plus數(shù)據(jù)權(quán)限實現(xiàn)代碼

    mybatis-plus數(shù)據(jù)權(quán)限實現(xiàn)代碼

    這篇文章主要介紹了mybatis-plus數(shù)據(jù)權(quán)限實現(xiàn),結(jié)合了mybatis-plus的插件方式,做出了自己的注解方式的數(shù)據(jù)權(quán)限,雖然可能存在一部分的局限性,但很好的解決了我們自己去解析SQL的功能,需要的朋友可以參考下
    2023-06-06
  • 分布式事務(wù)CAP兩階段提交及三階段提交詳解

    分布式事務(wù)CAP兩階段提交及三階段提交詳解

    這篇文章主要為大家介紹了分布式事務(wù)CAP、兩階段提交及三階段提交的內(nèi)容詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-01-01
  • javaweb 項目初始配置的方法步驟

    javaweb 項目初始配置的方法步驟

    本文主要介紹了javaweb 項目初始配置的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Springboot集成JAXB返回xml格式

    Springboot集成JAXB返回xml格式

    這篇文章主要為大家詳細介紹了Springboot如何集成JAXB返回xml格式,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-12-12
  • MybatisPlus中如何調(diào)用Oracle存儲過程

    MybatisPlus中如何調(diào)用Oracle存儲過程

    這篇文章主要介紹了MybatisPlus中如何調(diào)用Oracle存儲過程的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java 實現(xiàn)LZ78壓縮算法的示例代碼

    Java 實現(xiàn)LZ78壓縮算法的示例代碼

    這篇文章主要介紹了Java 實現(xiàn)LZ78壓縮算法的示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05

最新評論

吉木萨尔县| 蒲城县| 烟台市| 胶南市| 察隅县| 昔阳县| 台安县| 普陀区| 吴桥县| 迁西县| 西乌| 阜康市| 丰台区| 尼玛县| 长阳| 嘉义县| 三都| 措美县| 九龙县| 大足县| 新昌县| 江山市| 乌鲁木齐县| 邢台县| 玛多县| 怀集县| 泰和县| 石嘴山市| 仁化县| 建阳市| 棋牌| 利津县| 武安市| 益阳市| 涿鹿县| 秦安县| 灵山县| 甘肃省| 连云港市| 德江县| 定西市|