在Android中解析JSON数据

1.根据JSON数据格式新建class文件
JSON接口内容如下

{
    "baseinfo": {
        "city": "深圳",
        "date": "2018-09-12",
        "weekDay": "星期三",
        "publishTime": "2018-09-12 14:59:03"
    },
    "days": [{
        "city": "深圳",
        "weekDay": "星期三",
        "date": "2018-09-12",
        "Weather": "多云",
        "WEnum": "CLOUDY",
        "MaxDegree": 30,
        "MinDegree": 25,
        "Wind": "东风",
        "Flow": "3",
        "ClotheIndex": "",
        "ClotheAbstract": "",
        "ClotheDetail": "",
        "WashCar": "",
        "WashCar_l": "",
        "WashCar_s": "",
        "gm": "",
        "gm_l": "",
        "gm_s": "",
        "Ultravoilet": "",
        "Ultravoilet_l": "",
        "Ultravoilet_s": "",
        "Allergy": "",
        "sd": "",
        "yd": "",
        "yd_l": "",
        "yd_s": ""
    }, {
        "city": "深圳",
        "weekDay": "星期四",
        "date": "2018-09-13",
        "Weather": "雷阵雨",
        "WEnum": "RAIN_THUNDER",
        "MaxDegree": 31,
        "MinDegree": 25,
        "Wind": "无",
        "Flow": "1",
        "ClotheIndex": "",
        "ClotheAbstract": "",
        "ClotheDetail": "",
        "WashCar": "",
        "WashCar_l": "",
        "WashCar_s": "",
        "gm": "",
        "gm_l": "",
        "gm_s": "",
        "Ultravoilet": "",
        "Ultravoilet_l": "",
        "Ultravoilet_s": "",
        "Allergy": "",
        "sd": "",
        "yd": "",
        "yd_l": "",
        "yd_s": ""
    }, {
        "city": "深圳",
        "weekDay": "星期五",
        "date": "2018-09-14",
        "Weather": "多云",
        "WEnum": "CLOUDY",
        "MaxDegree": 33,
        "MinDegree": 26,
        "Wind": "无",
        "Flow": "1",
        "ClotheIndex": "",
        "ClotheAbstract": "",
        "ClotheDetail": "",
        "WashCar": "",
        "WashCar_l": "",
        "WashCar_s": "",
        "gm": "",
        "gm_l": "",
        "gm_s": "",
        "Ultravoilet": "",
        "Ultravoilet_l": "",
        "Ultravoilet_s": "",
        "Allergy": "",
        "sd": "",
        "yd": "",
        "yd_l": "",
        "yd_s": ""
    }, {
        "city": "深圳",
        "weekDay": "星期六",
        "date": "2018-09-15",
        "Weather": "阵雨",
        "WEnum": "RAIN_SHOWER",
        "MaxDegree": 34,
        "MinDegree": 24,
        "Wind": "无",
        "Flow": "1",
        "ClotheIndex": "",
        "ClotheAbstract": "",
        "ClotheDetail": "",
        "WashCar": "",
        "WashCar_l": "",
        "WashCar_s": "",
        "gm": "",
        "gm_l": "",
        "gm_s": "",
        "Ultravoilet": "",
        "Ultravoilet_l": "",
        "Ultravoilet_s": "",
        "Allergy": "",
        "sd": "",
        "yd": "",
        "yd_l": "",
        "yd_s": ""
    }, {
        "city": "深圳",
        "weekDay": "星期日",
        "date": "2018-09-16",
        "Weather": "暴雨",
        "WEnum": "RAIN_VERY_HEAVY",
        "MaxDegree": 27,
        "MinDegree": 24,
        "Wind": "东风",
        "Flow": "4",
        "ClotheIndex": "",
        "ClotheAbstract": "",
        "ClotheDetail": "",
        "WashCar": "",
        "WashCar_l": "",
        "WashCar_s": "",
        "gm": "",
        "gm_l": "",
        "gm_s": "",
        "Ultravoilet": "",
        "Ultravoilet_l": "",
        "Ultravoilet_s": "",
        "Allergy": "",
        "sd": "",
        "yd": "",
        "yd_l": "",
        "yd_s": ""
    }]
}

添加上述JSON数据对应的class文件:

package com.example.weatherinterface;

import java.util.List;

public class Weather {
    public baseinfo baseinfo;
    public List<day> days;

    public class baseinfo{
        public String city;
        public String date;
        public String weekDay;
        public String publishTime;
    }

    public class day {
        public String city;
        public String weekDay;
        public String date;
        public String Weather;
        public String WEnum;
        public int MaxDegree;
        public int MinDegree;
        public String Wind;
        public String Flow;
        public String ClotheIndex;
        public String ClotheAbstract;
        public String ClotheDetail;
        public String WashCar;
        public String WashCar_l;
        public String WashCar_s;
        public String gm;
        public String gm_l;
        public String gm_s;
        public String Ultravoilet;
        public String Ultravoilet_l;
        public String Ultravoilet_s;
        public String Allergy;
        public String sd;
        public String yd;
        public String yd_l;
        public String yd_s;
    }
}

2.在gradle中添加

implementation 'com.google.code.gson:gson:2.2.4'

3.在代码中解析JSON

//s中存放的是已经下载好的JSON数据
private void parseData(String s) {
        try {
            JSONObject jsonObject=new JSONObject(s);
            JSONObject baseinfo=jsonObject.getJSONObject("baseinfo");
            String city=baseinfo.getString("city");
            String date=baseinfo.getString("date");
            String weekday=baseinfo.getString("weekDay");
            String publishtime=baseinfo.getString("publishTime");
            //下面一步是方法内容
            refreshTextView("baseinfo解析结果:"+"\ncity是"+city+"\ndate是"+date+"\nweekDay是"+weekday+"\npublishTime是"+publishtime);
            JSONArray array=jsonObject.getJSONArray("days");
            refreshTextView("\ndays数组的解析结果:");
            int ii=array.length();
            for(int i=0;i<array.length();i++){
                JSONObject day=array.getJSONObject(i);
                String weather=day.getString("Weather");
                int MaxDegree=day.getInt("MaxDegree");
                int MinDegree=day.getInt("MinDegree");
                refreshTextView("\n第"+(i+1)+"组:"+"\nweather是"+weather+"\nMaxDegree是"+MaxDegree+"\nMinDegree是"+MinDegree);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

4.在textView中显示

private void refreshTextView(String s) {
        Message msg=new Message();
        msg.what=0;
        msg.obj=s;
        handler.handleMessage(msg);
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
           // String s=textView.getText().toString();
            switch (msg.what){
                case 0:
                    textView.append(msg.obj.toString());
                    break;
            }
        }
    };

5.运行结果:
这里写图片描述
6.注意事项:
在写class类的时候注意要与JSON中的键名完全之一,特别注意大小写!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值