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中的键名完全之一,特别注意大小写!!!