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数组中。

 

转载于:https://my.oschina.net/u/3668429/blog/3083540

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值