net.sf.json转化json字符串报错处理

阅读更多

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 map=new HashMap<>();
map.put("data", Map.class);
Map bean = (Map) JSONObject.toBean(fromObject1, Map.class,map);
Map bean2 = (Map) bean.get("data");//JSONArray.fromObject("[" + json.toString() + "]");
List couponList1 = (List) bean2.get("SpUsercouponVO");

 总结:在JSONObject.toBean(jsonObject1,class)的时候,如果class对象中包含其他对象类型的时候,最好做一个类型说明。

Map map=new HashMap<>();

map.put("data", Map.class);

JSONObject.toBean(jsonObject1,class,map),第三个参数。

 

 

你可能感兴趣的:(json,java,MorphDynaBean)