91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-08-28 23:24:05 來源:腳本之家 閱讀:174 作者:jihite 欄目:編程語言

前言

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

json數據

{

 "resultcode": "200",

 "reason": "successed!",

 "result": {

  "sk": {

   "temp": "24",

   "wind_direction": "西南風",

   "wind_strength": "2級",

   "humidity": "51%",

   "time": "10:11"

  },

  "today": {

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

   "weather": "陰轉多云",

   "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": "陰轉多云",

    "weather_id": {

     "fa": "02",

     "fb": "01"

    },

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

    "week": "星期四",

    "date": "20150604"

   },

   {

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

    "weather": "多云轉晴",

    "weather_id": {

     "fa": "01",

     "fb": "00"

    },

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

    "week": "星期五",

    "date": "20150605"

   },

   {

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

    "weather": "多云轉陰",

    "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();
  }
 }
}

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

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

總結

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

金堂县| 商南县| 松滋市| 鸡东县| 防城港市| 扶余县| 永安市| 阿鲁科尔沁旗| 科技| 赤壁市| 任丘市| 扎囊县| 集贤县| 霸州市| 界首市| 神农架林区| 福清市| 永城市| 西乌珠穆沁旗| 沁源县| 彩票| 游戏| 桃园县| 泸水县| 方正县| 大城县| 霍城县| 泽普县| 绥化市| 屏东县| 沈丘县| 东乡族自治县| 金乡县| 华池县| 云阳县| 射阳县| 剑阁县| 紫阳县| 睢宁县| 天津市| 登封市|