json boolean android,安卓JSON解析问题。

我们把json文件重新排列,目的是为了呈现其清晰的嵌套结构。因此,可以注意到,json包含这些元素:

由[]括起来的称之为数组JSONArray;

由{}括起来的称之为JSONObject;

字符串String;

boolean, double, int, long 基本数据类型;

通过JSON Object的方法获取这些数据,比如string:JSONObject.getString(String name)

然后根据JSON文件的结构,一层一层地解析:

public void parseWeatherObject(JSONObject object) throws JSONException {

if (object != null) {

JSONArray array = object.getJSONArray("HeWeather data service 3.0");

if (array != null && array.length() > 0) {

for (int i = 0; i < array.length(); i++) {

JSONObject weatherItem = array.getJSONObject(i);

JSONObject basic = weatherItem.getJSONObject("basic");

JSONArray dailyForecast = weatherItem.getJSONArray("daily_forecast");

JSONArray hourly_forecast = weatherItem.getJSONArray("hourly_forecast");

JSONObject now = weatherItem.getJSONObject("now");

// parse "now"

parseNowObject(now);

String status = weatherItem.getString("status");

JSONObject suggestion = weatherItem.getJSONObject("suggestion");

i=i;

}

}

}

}

private void parseNowObject(JSONObject now) throws JSONException {

if (now != null) {

JSONObject cond = now.getJSONObject("cond");

int condCode = cond.getInt("code");

String condTxt = cond.getString("txt");

int fl = now.getInt("fl");

int hum = now.getInt("hum");

String pcpn = now.getString("pcpn");

// ......

}

}

整理后的JSON文件:

{"HeWeather data service 3.0":

[

{

"basic":{

"city":"漯河",

"cnty":"中国",

"id":"CN101181501",

"lat":"33.563000",

"lon":"114.026000",

"update":{

"loc":"2015-09-15 14:46",

"utc":"2015-09-15 06:46"}

},

"daily_forecast":[{"astro":{"sr":"06:06","ss":"18:31"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-15","hum":"39","pcpn":"0.1","pop":"1","pres":"1016","tmp":{"max":"28","min":"13"},"vis":"10","wind":{"deg":"62","dir":"东风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:07","ss":"18:30"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-16","hum":"42","pcpn":"0.0","pop":"0","pres":"1016","tmp":{"max":"28","min":"16"},"vis":"10","wind":{"deg":"74","dir":"东北风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:08","ss":"18:28"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-17","hum":"41","pcpn":"0.0","pop":"0","pres":"1017","tmp":{"max":"27","min":"17"},"vis":"10","wind":{"deg":"137","dir":"东风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:08","ss":"18:27"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-18","hum":"36","pcpn":"0.0","pop":"1","pres":"1015","tmp":{"max":"28","min":"17"},"vis":"10","wind":{"deg":"117","dir":"无持续风向","sc":"微风","spd":"0"}},{"astro":{"sr":"06:09","ss":"18:26"},"cond":{"code_d":"101","code_n":"100","txt_d":"多云","txt_n":"晴"},"date":"2015-09-19","hum":"35","pcpn":"0.0","pop":"0","pres":"1014","tmp":{"max":"29","min":"17"},"vis":"10","wind":{"deg":"67","dir":"东北风","sc":"3-4","spd":"0"}},{"astro":{"sr":"06:10","ss":"18:24"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-20","hum":"31","pcpn":"0.0","pop":"0","pres":"1011","tmp":{"max":"30","min":"17"},"vis":"10","wind":{"deg":"121","dir":"无持续风向","sc":"微风","spd":"0"}},{"astro":{"sr":"06:10","ss":"18:23"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-21","hum":"30","pcpn":"0.0","pop":"0","pres":"1010","tmp":{"max":"30","min":"17"},"vis":"10","wind":{"deg":"157","dir":"无持续风向","sc":"微风","spd":"0"}}],

"hourly_forecast":[{"date":"2015-09-15 16:00","hum":"39","pop":"0","pres":"1015","tmp":"30","wind":{"deg":"60","dir":"东北风","sc":"微风","spd":"12"}},{"date":"2015-09-15 19:00","hum":"53","pop":"0","pres":"1015","tmp":"27","wind":{"deg":"68","dir":"东北风","sc":"微风","spd":"14"}},{"date":"2015-09-15 22:00","hum":"67","pop":"0","pres":"1017","tmp":"23","wind":{"deg":"72","dir":"东北风","sc":"微风","spd":"11"}}],

"now":{

"cond":{

"code":"100",

"txt":"晴"

},

"fl":"26",

"hum":"38",

"pcpn":"?",

"pres":"1018",

"tmp":"27",

"vis":"10",

"wind":{

"deg":"62",

"dir":"东北风",

"sc":"4-5",

"spd":"0"

}

},

"status":"ok",

"suggestion":{

"comf":{"brf":"较舒适","txt":"白天天气晴好,您在这种天气条件下,会感觉早晚凉爽、舒适,午后偏热。"},

"cw":{"brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},

"drsg":{"brf":"热","txt":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"},

"flu":{"brf":"少发","txt":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。"},

"sport":{"brf":"较适宜","txt":"天气较好,但考虑气温较低,推荐您进行室内运动,若户外适当增减衣物并注意防晒。"},

"trav":{"brf":"适宜","txt":"天气较好,温度适宜,是个好天气哦。这样的天气适宜旅游,您可以尽情地享受大自然的风光。"},

"uv":{"brf":"强","txt":"紫外线辐射强,建议涂擦SPF20左右、PA++的防晒护肤品。避免在10点至14点暴露于日光下。"}

}

}

]

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个SON解析封装类, public class ParsreTools { public static <T> T pasrsJsonStrSimpleT(String jsonString, String key, Class<T> cls) { T t = null; Field fields[] = cls.getDeclaredFields(); String varName[] = new String[fields.length]; for (int i = 0; i < fields.length; i++) { varName[i] = fields[i].getName(); } try { JSONObject jsonObject = new JSONObject(jsonString); JSONObject jsonObject2 = jsonObject.getJSONObject(key); t = cls.newInstance(); for (int i = 0; i < varName.length; i++) { Field field = cls.getDeclaredField(varName[i]); String type = field.getGenericType().toString(); Object object = new Object(); if (type.equals("int")) { object = jsonObject2.getInt(varName[i]); } else if (type.equals("double")) { object = jsonObject2.getDouble(varName[i]); } else if (type.equals("long")) { object = jsonObject2.getLong(varName[i]); } else if (type.equals("boolean")) { object = jsonObject2.getBoolean(varName[i]); } else if (type.equals("class java.lang.String")) { object = jsonObject2.getString(varName[i]); } else if (type.contains("java.util.List")) { int index1 = type.indexOf("<"); int index2 = type.indexOf(">"); type = type.substring(index1 + 1, index2); Class cls1 = Class.forName(type); String jString = jsonObject2.toString(); String key1 = varName[i]; object = pasrsJsonStrMultT(jString, key1, cls1); } else { type = type.replace("class ", ""); Class cls1 = Class.forName(type); String key1 = varName[i]; String jsString = jsonObject2.toString(); object = pasrsJsonStrSimpleT(jsString, key1, cls1); } field.setAccessible(true); field.set(t, object); } } catch (Exception e) { e.printStackTrace(); } return t; } public static <T> List<T> pasrsJsonStrMultT(String jsonString, String key, Class<T> cls) { List<T> list = new ArrayList<T>(); T t = null; Field fields[] = cls.getDeclaredFields(); String varName[] = new String[fields.length]; for (int i = 0; i < fields.length; i++) { varName[i] = fields[i].getName(); } try { JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { t = cls.newInstance(); JSONObject jObject = jsonArray.getJSONObject(i); for (int j = 0; j < varName.length; j++) { Field field = cls.getDeclaredField(varName[j]); String type = field.getGenericType().toString(); Object object = new Object(); if (type.equals("int")) { object = jObject.getInt(varName[j]); } else if (type.equals("double")) { object = jObject.getDouble(varName[j]); } else if (type.equals("long")) { object = jObject.getLong(varName[j]); } else if (type.equals("boolean")) { object = jObject.getBoolean(varName[j]); } else if (type.equals("class java.lang.String")) { object = jObject.getString(varName[j]); } else if (type.contains("java.util.List")) { int index1 = type.indexOf("<"); int index2 = type.indexOf(">"); type = type.substring(index1 + 1, index2); Class cls1 = Class.forName(type); String jString = jObject.toString(); String key1 = varName[j]; object = pasrsJsonStrMultT(jString, key1, cls1); } else { type = type.replace("class ", ""); Class cls1 = Class.forName(type); String key1 = varName[j]; String jsString = jObject.toString(); object = pasrsJsonStrSimpleT(jsString, key1, cls1); } field.setAccessible(true); field.set(t, object); } list.add(t); } } catch (Exception e) { e.printStackTrace(); } return list;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值