国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

淺談JSONObject解析JSON數據

mindwind / 3301人閱讀

摘要:感冒指數易發感冒容易發生,少去人群密集的場所有利于降低感冒的幾率。穿衣指數舒適白天溫度適中,但早晚涼,易穿脫的便攜外套很實用。運動指數不適宜受到陣雨天氣的影響,不宜在戶外運動。

個人博客同步文章 https://mr-houzi.com/2018/06/...
根據一段天氣API來說一下JSONObject如何解析json數據,盡管現在在開發中使用Gson等,對于像我這樣初次使用Java做開發的小白,說一下也是好的。
JSON數據

選取這段json數據是因為這段數據還是比較復雜的,能涵蓋要說的關鍵點

{
  "data": {
    "city": "深圳",
    "temphigh": "25",
    "templow": "19",
    "updatetime": "2017-11-04 13:23:00",
    "tempnow": "24",
    "sendibletemp": "27",
    "winddirect": "東北風",
    "windpower": "2級",
    "humidity": "42",
    "sunrise": "06:29",
    "sunset": "17:45",
    "weather": "多云",
    "week": "星期六",
    "nl": null,
    "date": "2017-11-04",
    "index": [
      {
        "name": "化妝指數",
        "level": "控油",
        "msg": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。"
      },
      {
        "name": "感冒指數",
        "level": "易發",
        "msg": "感冒容易發生,少去人群密集的場所有利于降低感冒的幾率。"
      },
      {
        "name": "洗車指數",
        "level": "不宜",
        "msg": "雨(雪)水和泥水會弄臟您的愛車,不適宜清洗車輛。"
      },
      {
        "name": "穿衣指數",
        "level": "舒適",
        "msg": "白天溫度適中,但早晚涼,易穿脫的便攜外套很實用。"
      },
      {
        "name": "紫外線強度指數",
        "level": "弱",
        "msg": "輻射較弱,涂擦SPF12-15、PA+護膚品。"
      },
      {
        "name": "運動指數",
        "level": "不適宜",
        "msg": "受到陣雨天氣的影響,不宜在戶外運動。"
      }
    ],
    "pm25": {
      "aqi": 0,
      "co": 8,
      "o3": 42,
      "pm10": 63,
      "pm2_5": 64,
      "quality": "良",
      "so2": 4,
      "no2": 11,
      "updatetime": "2017-11-04 13:00:00"
    },
    "daily": [
      {
        "date": "2017-11-04",
        "week": "星期六",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "25",
        "templow": "19",
        "weather": "多云"
      },
      {
        "date": "2017-11-05",
        "week": "星期日",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "26",
        "templow": "19",
        "weather": "多云"
      },
      {
        "date": "2017-11-06",
        "week": "星期一",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "27",
        "templow": "20",
        "weather": "多云"
      },
      {
        "date": "2017-11-07",
        "week": "星期二",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "28",
        "templow": "21",
        "weather": "多云"
      },
      {
        "date": "2017-11-08",
        "week": "星期三",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "29",
        "templow": "22",
        "weather": "多云"
      },
      {
        "date": "2017-11-09",
        "week": "星期四",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "28",
        "templow": "22",
        "weather": "多云"
      },
      {
        "date": "2017-11-03",
        "week": "星期五",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "28",
        "templow": "18",
        "weather": "晴"
      }
    ]
  },
  "status": 0,
  "msg": "ok"
}
解析JSON 利用JSONString進行簡單解析

我利用了RxVolley進行數據通信,t為API返回的數據

RxVolley.get("https://chkj02.market.alicloudapi.com/qgtq?city="+city, params, new HttpCallback() {
    @Override
    public void onSuccess(String t) {
        Loger.debug("請求到的數據:" + t);
    }
});

我們現在要獲取這部分數據,該如何進行解析呢?

首先,將t中的數據傳到JSONObject類型的jsonObject中,再通過getJSONObject獲取到data下的數據。

//解析數據
JSONObject jsonObject = new JSONObject(t);
JSONObject jsonData = jsonObject.getJSONObject("data");

此時,jsonData中數據為

{
    "city": "深圳",
    "temphigh": "25",
    "templow": "19",
    "updatetime": "2017-11-04 13:23:00",
    "tempnow": "24",
    "sendibletemp": "27",
    "winddirect": "東北風",
    "windpower": "2級",
    "humidity": "42",
    "sunrise": "06:29",
    "sunset": "17:45",
    "weather": "多云",
    "week": "星期六",
    "nl": null,
    "date": "2017-11-04"
}

然后通過getString進行讀值即可

//解析天氣
String jsonTemplow = jsonData.getString("templow");
String jsonTempHigh = jsonData.getString("temphigh");
String jsonWeather = jsonData.getString("weather");
String jsonTempnow = jsonData.getString("tempnow");
String jsonWinddirect = jsonData.getString("winddirect");
String jsonWindpower = jsonData.getString("windpower");
String jsonHumidity = jsonData.getString("humidity");
利用JSONArray進行復雜解析

這次,我們要獲取這部分數據

首先,將t中的數據傳到JSONObject類型的jsonObject中,再通過getJSONObject獲取到data下的數據。然后jsonArray通過getJSONArray獲得index下的數據

//解析數據
JSONObject jsonObject = new JSONObject(t);
JSONObject jsonData = jsonObject.getJSONObject("data");
JSONArray jsonIndex =jsonData.getJSONArray("index");
JSONArray jsonDaily =jsonData.getJSONArray("daily");

方法一

此時,jsonDaily中數據為

[
      {
        "date": "2017-11-04",
        "week": "星期六",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "25",
        "templow": "19",
        "weather": "多云"
      },
      {
        "date": "2017-11-05",
        "week": "星期日",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "26",
        "templow": "19",
        "weather": "多云"
      },
      {
        "date": "2017-11-06",
        "week": "星期一",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "27",
        "templow": "20",
        "weather": "多云"
      },
      {
        "date": "2017-11-07",
        "week": "星期二",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "28",
        "templow": "21",
        "weather": "多云"
      },
      {
        "date": "2017-11-08",
        "week": "星期三",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "29",
        "templow": "22",
        "weather": "多云"
      },
      {
        "date": "2017-11-09",
        "week": "星期四",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "28",
        "templow": "22",
        "weather": "多云"
      },
      {
        "date": "2017-11-03",
        "week": "星期五",
        "sunrise": "06:29",
        "sunset": "17:45",
        "temphigh": "28",
        "templow": "18",
        "weather": "晴"
      }
]

把jsonDaily中按分類進行解析,分為幾個ArrayList<>,datesweeksweathers等,然后進行for循環。

List dates = new ArrayList<>();
List weeks = new ArrayList<>();
List weathers = new ArrayList<>();

int j=1;
for (int i=0;i

方法二

此時,jsonIndex中數據為

 [
      {
        "name": "化妝指數",
        "level": "控油",
        "msg": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。"
      },
      {
        "name": "感冒指數",
        "level": "易發",
        "msg": "感冒容易發生,少去人群密集的場所有利于降低感冒的幾率。"
      },
      {
        "name": "洗車指數",
        "level": "不宜",
        "msg": "雨(雪)水和泥水會弄臟您的愛車,不適宜清洗車輛。"
      },
      {
        "name": "穿衣指數",
        "level": "舒適",
        "msg": "白天溫度適中,但早晚涼,易穿脫的便攜外套很實用。"
      },
      {
        "name": "紫外線強度指數",
        "level": "弱",
        "msg": "輻射較弱,涂擦SPF12-15、PA+護膚品。"
      },
      {
        "name": "運動指數",
        "level": "不適宜",
        "msg": "受到陣雨天氣的影響,不宜在戶外運動。"
      }
]

jsonArray為二維數組,我們通過兩個嵌套循環進行遍歷。首先,外層根據數組長度進行for循環遍歷;然后內層使用迭代器進行遍歷。

String[] jsonIndex = new String[20];//數組長度聲明為20確保夠用
int j=1;
for (int i=0;i

這樣此指數數據就被我們成功解析,然后存入jsonIndex數組中。

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/71311.html

相關文章

  • java解析和創建JSON

    摘要:概述即,是對象表示法的子集。具有以下特點數據放在鍵值對中數據由逗號分隔花括號表示對象方括號表示數組。創建一個對象為對象添加屬性創建數組將對象添加到數組將數組添加到對象將對象轉化成字符串參考文檔官網 JSON概述 JSON即javascript object notation,是javascript對象表示法的子集。具有以下特點: 數據放在鍵值對中; 數據由逗號分隔; 花括號表示對...

    鄒強 評論0 收藏0
  • 慕課網_《JSON快速入門(Java版)》學習總結

    摘要:時間年月日星期日說明本文部分內容均來自慕課網。慕課網教學示例源碼無個人學習源碼第一章課程概述課程介紹課程須知本課程面向所有使用語言進行開發的小伙伴。 時間:2017年05月21日星期日說明:本文部分內容均來自慕課網。@慕課網:http://www.imooc.com教學示例源碼:無個人學習源碼:https://github.com/zccodere/s... 第一章:課程概述 1-1 ...

    shiina 評論0 收藏0

發表評論

0條評論

mindwind

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<