android Json解析
来源:互联网 发布:java项目管理 编辑:程序博客网 时间:2024/06/03 10:20
1.需解析的json文件(中括号[]括起来的是json数组)
{ "HeWeather data service 3.0": [ { "aqi": { "city": { "aqi": "28", "co": "0", "no2": "15", "o3": "94", "pm10": "25", "pm25": "13", "qlty": "优", "so2": "2" } }, "basic": { "city": "北京", "cnty": "中国", "id": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": { "loc": "2016-09-05 17:52", "utc": "2016-09-05 09:52" } }, "daily_forecast": [ { "astro": { "sr": "05:46", "ss": "18:39" }, "cond": { "code_d": "100", "code_n": "101", "txt_d": "晴", "txt_n": "多云" }, "date": "2016-09-05", "hum": "26", "pcpn": "0.7", "pop": "93", "pres": "1009", "tmp": { "max": "31", "min": "20" }, "vis": "10", "wind": { "deg": "294", "dir": "无持续风向", "sc": "微风", "spd": "6" } }, ...
2.需获取的数据
"wind_sc":"微风","wind_dir":"无持续风向""ss":"18:41","sr":"05:46""tmp":{"min":"18","max":"30"},"txt_d":"雷阵雨""txt_n":"多云"
3.创建类Item
package app.coolwhether.com.weatherapp.entity;/** * Created by kirito on 2016/9/3. */public class Item { private String wind_sc; private String wind_dir; private String tmp_min; private String tmp_max; private String txt_d; private String txt_n; private String ss; private String sr; private String date; public String getWind_sc() { return wind_sc; } public void setWind_sc(String wind_sc) { this.wind_sc = wind_sc; } public String getWind_dir() { return wind_dir; } public void setWind_dir(String wind_dir) { this.wind_dir = wind_dir; } public String getTmp_min() { return tmp_min; } public void setTmp_min(String tmp_min) { this.tmp_min = tmp_min; } public String getTxt_d() { return txt_d; } public void setTxt_d(String txt_d) { this.txt_d = txt_d; } public String getTxt_n() { return txt_n; } public void setTxt_n(String txt_n) { this.txt_n = txt_n; } public String getTmp_max() { return tmp_max; } public void setTmp_max(String tmp_max) { this.tmp_max = tmp_max; } public String getSs() { return ss; } public void setSs(String ss) { this.ss = ss; } public String getSr() { return sr; } public void setSr(String sr) { this.sr = sr; } public String getDate() { return date; } public void setDate(String date) { this.date = date; }}4.从URL获取数据(此处参照:android从URL获取数据)
5.json解析类
package app.coolwhether.com.weatherapp.support;import android.util.Log;import com.google.gson.Gson;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.util.ArrayList;import java.util.List;import app.coolwhether.com.weatherapp.entity.Item;/** * Created by kirito on 2016/9/3. */public class JsonHelper { private static final String TAG = "JsonHelper"; public static Item parseJsonToItem(String json) throws JSONException{ JSONObject jsonObject = new JSONObject(json); JSONArray heWeather = jsonObject.getJSONArray("HeWeather data service 3.0"); //Log.e(TAG, "parseJsonToList: ---heWeather"+heWeather); Item item = new Item(); JSONObject object = heWeather.getJSONObject(0);//0或是其它数字(数列heWeather的下标都可以) JSONArray daily_forecast = null; if (object.has("daily_forecast")){ daily_forecast = object.getJSONArray("daily_forecast"); //Log.e(TAG, "parseJsonToList: daily_forecast---"+daily_forecast ); JSONObject obj = daily_forecast.getJSONObject(0); JSONObject wind = null; if (obj.has("wind")){ wind = obj.getJSONObject("wind"); //Log.e(TAG, "parseJsonToList: wind---"+wind ); item.setWind_dir(wind.optString("dir")); item.setWind_sc(wind.optString("sc")); //Log.e(TAG, "parseJsonToList: dir,sc"+ wind.optString("dir")); } JSONObject astro = null; if (obj.has("astro")){ astro = obj.getJSONObject("astro"); item.setSr(astro.optString("sr")); item.setSs(astro.optString("ss")); //Log.e(TAG, "parseJsonToItem: ss---"+ astro.optString("ss")); } JSONObject temp = null; if (obj.has("tmp")){ temp = obj.getJSONObject("tmp"); item.setTmp_max(temp.optString("max")); item.setTmp_min(temp.optString("min")); //Log.e(TAG, "parseJsonToItem: max---"+ temp.optString("max")); } if(obj.has("date")){ item.setDate(obj.optString("date")); //Log.e(TAG, "parseJsonToItem: date---"+ obj.optString("date")); } if (obj.has("cond")){ JSONObject cond = obj.getJSONObject("cond"); item.setTxt_d(cond.optString("txt_d")); item.setTxt_n(cond.optString("txt_n")); //Log.e(TAG, "parseJsonToItem: txt_d---"+ cond.optString("txt_d")); } } return item; }}
6.调用的方法
item = JsonHelper.parseJsonToItem(s);//s为URL处获取的json数据
7.然后即可通过返回的item获取需要的数据.
0 0
- 【json】android json解析
- Android-Android解析JSON
- Android数据解析:Json解析
- android---Json数据解析
- Android Json 数据解析
- android 解析JSON
- android 解析json数据格式
- android 解析json数据格式
- Android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- Android的JSON解析
- Android 解析Json
- android JSON 解析
- Android 解析json数据格式
- android解析Json
- Android解析json数据
- Android Json解析
- 小米净水器
- Leetcode_3
- U-Boot启动阶段修改启动参数方法及分析
- android 最新技术文章
- 刚刚开通博客,希望在这里可以学习到更多的东西
- android Json解析
- Bash Shell 快捷键
- 十进制转二进制
- APUE可靠信号与不可靠信号
- Delphi中添加WebBrowser扩展接口
- kafka 配置项 server.properties
- Linux 删除文件夹和文件的命令
- OOD的原则
- json错误处理org.json.JSONException: No value for XXX