关于解析json数组中的json数组

来源:互联网 发布:复式组合软件 编辑:程序博客网 时间:2024/06/06 02:38

   大多数的人应该都知道怎么解析json数组,可今天却遇到一个json数组里面嵌套json数组的情况,废话不多说,上代码
JSONObject root = new JSONObject(jsondata);
JSONArray array1=root.getJSONArray("weather");//先将jsondata里面的weather数组提取出来
String dsa=array1.toString();
String asd=dsa.substring(1, dsa.length()-1);//这是最关键的,将weather的value去除【】后变成一个新的json                                                                            格式字符串,缺少这一步运行时会报错
JSONObject rJsonObject=new JSONObject(asd);//继续按照解析jsonarray的方式解析
JSONArray array=rJsonObject.getJSONArray("future");
for (int i = 0; i < array.length(); i++) {
JSONObject lan=array.getJSONObject(i);
System.out.println(lan.getString("date"));
System.out.println(lan.getString("day"));
}
jsondata的内容:
{"status":"OK","weather":[{"city_name":"北京","city_id":"CHBJ000000","last_update":"2015-05-06T22:58:53+08:00","future":[{"date":"2015-05-06","day":"周三","text":"小雨","code1":"13","code2":"13","high":"25","low":"11","cop":"0%","wind":"南风3~4级"},{"date":"2015-05-07","day":"周四","text":"晴/阵雨","code1":"0","code2":"10","high":"25","low":"14","cop":"0%","wind":"北风3~4级转微风小于3级"}]}]}


0 0