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

琴音飘渺 2015-04-22 05:53:35
求解析下面这段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": "操作执行成功"
}
}
}
...全文
4280 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_32374313 2015-10-28
  • 打赏
  • 举报
回复
CalendarInfo 是 什么
只为搞笑 2015-04-23
  • 打赏
  • 举报
回复
嘿嘿,给你个最简便的方法,你先对应数据格式把对象建好,比如像这样

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包是目前公认较快的。
budworm 2015-04-23
  • 打赏
  • 举报
回复
例子: {"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,有实体对象就能直接转换,也很方便。 猛戳这里!
雪月弦歌 2015-04-23
  • 打赏
  • 举报
回复
引用 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不确定经常变换。
琴音飘渺 2015-04-23
  • 打赏
  • 举报
回复
[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} ] } } 这种的我懂呀 问题是数组下面的那层数组呢 如果在进行循环的话 有些地方会出错
雪月弦歌 2015-04-22
  • 打赏
  • 举报
回复 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); }
失落夏天 版主 2015-04-22
  • 打赏
  • 举报
回复
用谷歌的一个Gson解析包吧。其他的JSON解析包和android互斥。。 要么就自己解析。 如果是我并且仅用一次,我会用正则提出来。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧