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

資訊專欄INFORMATION COLUMN

淺談JSONObject解析JSON數(shù)據(jù)

mindwind / 3308人閱讀

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

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

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

{
  "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": "化妝指數(shù)",
        "level": "控油",
        "msg": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。"
      },
      {
        "name": "感冒指數(shù)",
        "level": "易發(fā)",
        "msg": "感冒容易發(fā)生,少去人群密集的場所有利于降低感冒的幾率。"
      },
      {
        "name": "洗車指數(shù)",
        "level": "不宜",
        "msg": "雨(雪)水和泥水會弄臟您的愛車,不適宜清洗車輛。"
      },
      {
        "name": "穿衣指數(shù)",
        "level": "舒適",
        "msg": "白天溫度適中,但早晚涼,易穿脫的便攜外套很實用。"
      },
      {
        "name": "紫外線強度指數(shù)",
        "level": "弱",
        "msg": "輻射較弱,涂擦SPF12-15、PA+護膚品。"
      },
      {
        "name": "運動指數(shù)",
        "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進行數(shù)據(jù)通信,t為API返回的數(shù)據(jù)

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

我們現(xiàn)在要獲取這部分數(shù)據(jù),該如何進行解析呢?

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

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

此時,jsonData中數(shù)據(jù)為

{
    "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進行復雜解析

這次,我們要獲取這部分數(shù)據(jù)

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

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

方法一

此時,jsonDaily中數(shù)據(jù)為

[
      {
        "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循環(huán)。

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

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

方法二

此時,jsonIndex中數(shù)據(jù)為

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

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

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

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

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

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

相關文章

  • java解析和創(chuàng)建JSON

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

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

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

    shiina 評論0 收藏0

發(fā)表評論

0條評論

mindwind

|高級講師

TA的文章

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