当前位置: 代码迷 >> Android >> Android JSON 多重嵌套对象数组解析,该怎么处理
  详细解决方案

Android JSON 多重嵌套对象数组解析,该怎么处理

热度:62   发布时间:2016-04-27 22:29:41.0
Android JSON 多重嵌套对象数组解析
求解析下面这段JSON  红色是需要得到的值  急求!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!用了不少方法 可是总是有错 求简单有效的方法 最好是能够用代码来解释 表示不要只给思路 现在思路一大推


{
    "RspMessage": {
        "Body": {
            "orderHeads": {
                "com.qthd.deal.server.client.vo.OrderHead": [
                    {
                        "linkAddr": "成都市",
                        "orderDetails": {
                            "com.qthd.deal.server.client.vo.OrderDetail": [
                                {
                                    "proId": "22",
                                    "proName": "巴西咖啡豆",
                                    "proType": "4",
                                    "proCount": "100",
                                    "salePrice": "16"

                                },
                                {
                                    "proId": "102",
                                    "proName": "意式浓缩(单孔)",
                                    "proType": "1",
                                    "proCount": "2",
                                    "salePrice": "2000"

                                }
                            ]
                        },
                        "LinkTel": "18981825090",
                        "orderNum": "SD2014120900000002",
                        "addDate": "2014-12-0910:51:41.0"

                    },
                    {
                        "linkAddr": "成都市",
                        "orderDetails": {
                            "com.qthd.deal.server.client.vo.OrderDetail": [
                                {
                                    "proId": "22",
                                    "proName": "巴西咖啡豆",
                                    "proType": "4",
                                    "proCount": "100",
                                    "salePrice": "16"

                                },
                                {
                                    "proId": "102",
                                    "proName": "意式浓缩(单孔)",
                                    "proType": "1",
                                    "proCount": "2",
                                    "salePrice": "2000"

                                }
                            ]
                        },
                        "LinkTel": "18981825090",
                        "orderNum": "SD2014120900000001",
                        "addDate": "2014-12-0910:40:51.0"

                    }
                ]
            }
        },
        "Head": {
            "returnCode": "DEAL0000",
            "returnMessage": "操作执行成功"
        }
    }
}
------解决思路----------------------
栗子:
{"calendar": 
    {"calendarlist": 
            [ 
            {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, 
            {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false} 
            ] 
    } 
}

JSON转换
JSONObject jsonObject = new JSONObject(builder.toString()) 
                            .getJSONObject("calendar"); 
                    JSONArray jsonArray = jsonObject.getJSONArray("calendarlist"); 
                    for(int i=0;i<jsonArray.length();i++){ 
                        JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i); 
                        CalendarInfo calendarInfo = new CalendarInfo(); 
                        calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id")); 
                        calendarInfo.setTitle(jsonObject2.getString("title")); 
                        calendarInfo.setCategory_name(jsonObject2.getString("category_name")); 
                        calendarInfo.setShowtime(jsonObject2.getString("showtime")); 
                        calendarInfo.setEndtime(jsonObject2.getString("endshowtime")); 
                        calendarInfo.setAllDay(jsonObject2.getBoolean("allDay")); 
                        calendarInfos.add(calendarInfo); 
                    } 
------解决思路----------------------
引用:
Quote: 引用:

栗子:
{"calendar": 
    {"calendarlist": 
            [ 
            {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, 
            {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false} 
            ] 
    } 
}


这种的我懂呀 问题是数组下面的那层数组呢 如果在进行循环的话   有些地方会出错




是{}这种类型的就转成JSONObject,是[] 类型就转成  JSONArray,不会有错,除非有字段的key不确定经常变换。
------解决思路----------------------
例子:
{"calendar": 
    {"calendarlist": 
            [ 
            {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, 
            {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false} 
            ] 
    } 
}

JSON转换
JSONObject jsonObject = new JSONObject(builder.toString()) 
                            .getJSONObject("calendar"); 
                    JSONArray jsonArray = jsonObject.getJSONArray("calendarlist"); 
                    for(int i=0;i<jsonArray.length();i++){ 
                        JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i); 
                        CalendarInfo calendarInfo = new CalendarInfo(); 
                        calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id")); 
                        calendarInfo.setTitle(jsonObject2.getString("title")); 
                        calendarInfo.setCategory_name(jsonObject2.getString("category_name")); 
                        calendarInfo.setShowtime(jsonObject2.getString("showtime")); 
                        calendarInfo.setEndtime(jsonObject2.getString("endshowtime")); 
                        calendarInfo.setAllDay(jsonObject2.getBoolean("allDay")); 
                        calendarInfos.add(calendarInfo); 
                    } 

[]类型的是JSONArray  ;{}类型的是JSONObject。不能一把转换,例子楼上有。

你要是怕麻烦,也可以使用Gson,有实体对象就能直接转换,也很方便。


猛戳这里!
------解决思路----------------------
嘿嘿,给你个最简便的方法,你先对应数据格式把对象建好,比如像这样

public class PatientInfo {
public String dischargeDate;
public String returnvisitDate;
public String doctorNote;
public String id;
public List<String> medicines; 
public Doctor doctor;// 
}

这个对象里就有基本数据类型,也有list,还有一个doctor对象。
然后用FastJSON这个jar里的JSONObject.parseObject方法可以直接解析成一个对象,该json解析jar包是目前公认较快的。