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<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),第三个参数。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值