资讯专栏INFORMATION COLUMN

浅谈JSONObject解析JSON数据

mindwind / 3269人阅读

摘要:感冒指数易发感冒容易发生,少去人群密集的场所有利于降低感冒的几率。穿衣指数舒适白天温度适中,但早晚凉,易穿脱的便携外套很实用。运动指数不适宜受到阵雨天气的影响,不宜在户外运动。

个人博客同步文章 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数组中。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/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元查看
<