Android JSON 多重嵌套对象数组解析

时间:2022-09-15 12:37:06
求解析下面这段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": "操作执行成功"
        }
    }
}

7 个解决方案

#1


用谷歌的一个Gson解析包吧。其他的JSON解析包和android互斥。。
要么就自己解析。
如果是我并且仅用一次,我会用正则提出来。

#2


栗子:
{"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); 
                    } 

#3


Quote: 引用 2 楼 li352558693 的回复:

栗子:
{"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} 
            ] 
    } 
}


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

#4


引用 3 楼 u014509097 的回复:
Quote: 引用 2 楼 li352558693 的回复:

栗子:
{"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不确定经常变换。

#5


例子:
{"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,有实体对象就能直接转换,也很方便。


猛戳这里!

#6


嘿嘿,给你个最简便的方法,你先对应数据格式把对象建好,比如像这样

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包是目前公认较快的。

#7


CalendarInfo 是 什么

#1


用谷歌的一个Gson解析包吧。其他的JSON解析包和android互斥。。
要么就自己解析。
如果是我并且仅用一次,我会用正则提出来。

#2


栗子:
{"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); 
                    } 

#3


Quote: 引用 2 楼 li352558693 的回复:

栗子:
{"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} 
            ] 
    } 
}


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

#4


引用 3 楼 u014509097 的回复:
Quote: 引用 2 楼 li352558693 的回复:

栗子:
{"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不确定经常变换。

#5


例子:
{"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,有实体对象就能直接转换,也很方便。


猛戳这里!

#6


嘿嘿,给你个最简便的方法,你先对应数据格式把对象建好,比如像这样

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包是目前公认较快的。

#7


CalendarInfo 是 什么