欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

net.sf.json转化json字符串报错处理 博客分类: JSON jsonjavaMorphDynaBean 

程序员文章站 2024-02-24 16:08:46
...

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