android JSON解析数据 android解析天气预报
程序员文章站
2023-11-11 09:02:04
概要
笔者近期做到对天气预报json数据解析,在此小记。
天气预报接口:http://wthrcdn.etouch.cn/weather_mini?cit...
概要
笔者近期做到对天气预报json数据解析,在此小记。
天气预报接口:http://wthrcdn.etouch.cn/weather_mini?citykey=101200101
json数据如下:
{ "desc": "ok", "status": 1000, "data": { "wendu": "14", "ganmao": "天气转凉,空气湿度较大,较易发生感冒,体质较弱的朋友请注意适当防护。", "forecast": [ { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 17℃", "type": "小雨", "low": "低温 10℃", "date": "30日星期四" }, { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 18℃", "type": "多云", "low": "低温 7℃", "date": "31日星期五" }, { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 20℃", "type": "晴", "low": "低温 8℃", "date": "1日星期六" }, { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 23℃", "type": "晴", "low": "低温 10℃", "date": "2日星期天" }, { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 23℃", "type": "多云", "low": "低温 12℃", "date": "3日星期一" } ], "yesterday": { "fl": "微风", "fx": "无持续风向", "high": "高温 21℃", "type": "阴", "low": "低温 12℃", "date": "29日星期三" }, "aqi": "114", "city": "武汉" } }
最终解析效果:
解析概述
1、首先,接到的整个数据可以转化为jsonobject对象。
2、通过整个数据的jsonobject对象获取到data中的数据,也是一个jsonobject对象。在data中就可以获取到此时温度,以及城市等信息。
3、通过data的jsonobject对象可以获取到forecast中的数据,forecast中的数据则是一个jsonarray对象。
4、通过forecast的jsonarray对象可以获取到近几天的天气信息,每一条为一个jsonobject对象。
代码
方便起见,笔者使用了volley框架,读者新建项目需要在build.gradle的dependencies中添加如下:
compile 'eu.the4thfloor.volley:com.android.volley:2015.05.28'
mainactivity.java:
package com.example.double2.jsontext; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.widget.textview; import com.android.volley.requestqueue; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.jsonobjectrequest; import com.android.volley.toolbox.volley; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; public class mainactivity extends appcompatactivity { private textview tvmain; private requestqueue mrequestqueue; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); } private void initview() { tvmain = (textview) findviewbyid(r.id.tv_main); mrequestqueue = volley.newrequestqueue(this); jsonobjectrequest mjsonobjectrequest = new jsonobjectrequest( "http://wthrcdn.etouch.cn/weather_mini?citykey=101200101", null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { try { jsonobject data = new jsonobject(response.getstring("data")); jsonarray forecast = data.getjsonarray("forecast"); jsonobject todayweather = forecast.getjsonobject(0); string wendu = data.getstring("wendu") + "\n"; string ganmao = data.getstring("ganmao") + "\n"; string high = todayweather.getstring("high") + "\n"; string low = todayweather.getstring("low") + "\n"; string date = todayweather.getstring("date") + "\n"; string city = data.getstring("city") + "\n"; tvmain.settext(wendu + ganmao + high + low + date+city); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.e("tag", error.getmessage(), error); } }); mrequestqueue.add(mjsonobjectrequest); } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <textview android:id="@+id/tv_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" /> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。