第一种只是key不同
{ "errno": 0, "errmsg": "success", "data": { "5": [ { "log_id": "88904874", "ext_flag": "0", "createTime": "2017-04-16 11:12:30", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "创建了该运单(17040001) 开单时间:2017-04-16 11:12:45", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ], "20": [ { "log_id": "88904873", "ext_flag": "0", "createTime": "2017-04-16 08:12:52", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "创建了到网点小黑的运单", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ], "200": [ { "log_id": "88904875", "ext_flag": "0", "createTime": "2017-04-16 10:12:52", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "该运单由南昌01 王文明操作订单(订单号:W170400003)转运单生成", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ], "160": [ { "log_id": "88904873", "ext_flag": "0", "createTime": "2017-04-16 11:14:52", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "创建了到网点小黑的运单", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ], "8130": [ { "log_id": "88904873", "ext_flag": "0", "createTime": "2017-04-16 11:12:52", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "创建了到网点小黑的运单", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ], "8370": [ { "log_id": "88904873", "ext_flag": "0", "createTime": "2017-02-16 11:12:52", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "创建了到网点小黑的运单", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ], "8030": [ { "log_id": "88904873", "ext_flag": "0", "createTime": "2017-03-16 11:12:52", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "创建了到网点小黑的运单", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ] } }可使用如下方式解析 JSONObject result = new JSONObject(response); LogInfo logInfo = new LogInfo(); Gson gson = new Gson(); Map<String, List<LogInfo.LogInfoItem>> logInfoMap = gson.fromJson(result.getJSONObject("data").toString(), new TypeToken<Map<String, List<LogInfo.LogInfoItem>>>() { }.getType()); for (Map.Entry listEntry : logInfoMap.entrySet()) { String type = LogInfoType.initType((String) listEntry.getKey()); for (LogInfo.LogInfoItem item : (List<LogInfo.LogInfoItem>) listEntry.getValue()) { item.type = TextUtils.isEmpty(type) ? "" : type; logInfo.list.add(item); } }不要问我LogInfo LogInfo.LogInfoItem是啥 也可以使用手动方式解析 JSONObject result = new JSONObject(response); LogInfo logInfo = new LogInfo(); JSONObject data = result.getJSONObject("data"); Iterator<String> iterator = data.keys(); while (iterator.hasNext()) { String key = iterator.next(); String type = LogInfoType.initType(key); JSONArray jsonArray = data.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); LogInfo.LogInfoItem item = LogInfo.LogInfoItem.objectFromData(jsonObject.toString()); item.type = type; item.trans = LogInfo.TransInfo.objectFromData(jsonObject.getJSONObject("transInfo").toString()); logInfo.list.add(item); } }
第二种json就比较恶心了,
{ "errno": 0, "errmsg": "success", "data": { "5": [ { "log_id": "88905577", "ext_flag": "0", "createTime": "2017-05-04 11:15:53", "companyName": "南昌01", "userName": "王文明", "telePhone": "", "thing": "创建了该运单(17050001) 开单时间:2017-05-04 11:15:13", "applicant": "", "modifyReason": "", "multifunc": "", "reserved": "", "from": 0, "payMode": "", "transInfo": [], "msg_infos": [] } ], "20": [ { "log_id": "88905578", "ext_flag": "0", "createTime": "2017-05-04 11:16:33", "companyName": "18811596967", "userName": "王文明", "telePhone": "18811596967", "thing": 0, "applicant": "", "modifyReason": "", "multifunc": "转交不可见", "reserved": "", "from": 0, "payMode": "现付", "transInfo": { "companyName": "18811596967", "transTime": "2017-05-04 11:16:22", "transOrderNum": "", "transStatus": "转交不可见", "payMode": "现付", "transType": "10", "transPrice": "0", "telePhone": "18811596967", "payOver": "0", "transRemark": "", "tName": null, "tPhone": "", "tName1": null, "tPhone1": "", "tName2": null, "tPhone2": "", "transfer_pick_goods_price": 0, "transfer_delivery_price": 0, "transfer_freight_price": 0, "transfer_handling_price": 0, "transfer_misc_price": 0, "transfer_local_pick_goods_price": 0, "discount": 0, "trans_batch_num": "ZJ2299917050001", "pay_arrival": 0, "transfer_pay_over": 0, "transfer_price_received": 0, "show_log_detail": 1, "origin_point_order_id": "18055747", "transfer_traffic_price": "", "car_number": "", "driver_name": "", "driver_phone": "" }, "msg_infos": [] } ] } }当遇到这种情况,尽量与好基友沟通,如果实在是这种格式,一定要镇定,万万不可一言不合就爆他菊花 这种格式一出现,正常的解析方式肯定是不可以的了,只能自己手动解析,经楼主验证,这种格式不可用这种JSON解析类去解析, 只能用Json打头的小写的解析类去解析,原因请往下看 解析方法来了啊 JSONObject result = new JSONObject(response); LogInfo logInfo = new LogInfo(); JSONObject data = result.getJSONObject("data"); Gson gson = new Gson(); JsonObject jsonData = gson.fromJson(data.toString(), JsonObject.class); for (Map.Entry entry : jsonData.entrySet()) { String key = (String) entry.getKey(); String type = LogInfoType.initType(key); JsonArray jsonArray = jsonData.getAsJsonArray(key); for (int i = 0; i < jsonArray.size(); i++) { JsonObject jsonObject = (JsonObject) jsonArray.get(i); LogInfo.LogInfoItem item = LogInfo.LogInfoItem.objectFromData(jsonObject.toString()); item.type = type; JsonElement jsonTrans = jsonObject.get("transInfo"); if (jsonTrans.isJsonArray()) { item.trans = null; } if (jsonTrans.isJsonObject()) { item.trans = LogInfo.TransInfo.objectFromData(jsonTrans.toString()); } logInfo.list.add(item); } } 因为大写的Json解析类没有判断数组和对象的方法