1. 程式人生 > >JSONObject解析JSON資料

JSONObject解析JSON資料

通過http請求獲取返回結果,結果型別為string,先轉換為json,

JSONObject jsonObject=JSONObject.parseObject(result);

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進行簡單解析

獲取如下內容,如何解析?

{
  "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",

 通過getJSONObject獲取到data下的資料。

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

獲取這部分資料

"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": "晴"
      }
    ]

通過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<String> dates = new ArrayList<>();
List<String> weeks = new ArrayList<>();
List<String> weathers = new ArrayList<>();

int j=1;
for (int i=0;i<jsonArray.length();i++){
    JSONObject partDaily = jsonArray.getJSONObject(i);
    JSONString date = partDaily.getString("date");
    dates.add(date);
    JSONString week = partDaily.getString("week");
    weeks.add(week);
    JSONString weather = partDaily.getString("weather");
    weathers.add(weather);
}

方法二

此時,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<jsonArray.length();i++){
    JSONObject partIndex = jsonArray.getJSONObject(i);
    Iterator iterator = partIndex.keys();
    String key;
    while(iterator.hasNext()){
        //hasNext方法,只是判斷下一個元素的有無,並不移動指標
        key = (String) iterator.next();//next方法,向下移動指標,並且返回指標指向的元素,如果指標指向的記憶體中沒有元素,會報異常
        jsonIndex[j] = partIndex.getString(key);
        j++;
    }
}

這樣此指數資料就被我們成功解析,然後存入jsonIndex陣列