1.json字符串
{
"total": 2,
"status": true,
"data": {
"SpUsercouponVO": [
{
"parkLimit": 0,
"createTime": null,
"phone": "18310642222",
"refUserName": null,
"synStatus": 0,
"areaLimit": 0,
"useLimit": 0,
"userId": "0c0c8c16_7d60_11e5_a40d_44a8422565ee",
"isGet": null,
"usercouponId": "0c0df166_7d60_11e5_a40d_44a8422565ee",
"maxValue": 0,
"couponName": null,
"useType": 1,
"couponType": 0,
"couponCode": null,
"toDate": "2015-10-29 00:00:00",
"couponId": "0361805e-8776-11e5-800e-822ace0c0095",
"isExpired": 0,
"minMoney": 111,
"fromDate": "2015-10-28 00:00:00",
"source": "首次登陆",
"isUserd": 0,
"day": 12,
"cValue": 11
},
{
"parkLimit": 0,
"createTime": null,
"phone": "18310642222",
"refUserName": null,
"synStatus": 0,
"areaLimit": 0,
"useLimit": 0,
"userId": "0c0c8c16_7d60_11e5_a40d_44a8422565ee",
"isGet": null,
"usercouponId": "a3604bf0_7d64_11e5_a40d_44a8422565ee",
"maxValue": 0,
"couponName": null,
"useType": 1,
"couponType": 0,
"couponCode": null,
"toDate": "2015-10-29 00:00:00",
"couponId": "0361805e-8776-11e5-800e-822ace0c0095",
"isExpired": 0,
"minMoney": 111,
"fromDate": "2015-10-28 00:00:00",
"source": "首次绑定车辆",
"isUserd": 0,
"day": 12,
"cValue": 11
}
]
},
"code": "0000",
"msg": "操作成功!"
}
2.json字符串转换为json对象
3.报错:Exception in thread "main" java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to java.util.Map
JSONObject fromObject1 = JSONObject.fromObject(sendHttpPOST1); @SuppressWarnings("unchecked") Map<String,Class> map=new HashMap<>(); map.put("data", Map.class); Map<String,Object> bean = (Map<String, Object>) JSONObject.toBean(fromObject1, Map.class,map); Map<String,Object> bean2 = (Map<String,Object>) bean.get("data");//JSONArray.fromObject("[" + json.toString() + "]"); List<UserCouponVO> couponList1 = (List<UserCouponVO>) bean2.get("SpUsercouponVO");
总结:在JSONObject.toBean(jsonObject1,class)的时候,如果class对象中包含其他对象类型的时候,最好做一个类型说明。
Map<String,Class> map=new HashMap<>();
map.put("data", Map.class);
在JSONObject.toBean(jsonObject1,class,map),第三个参数。