當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
解决问题 com.alibaba.fastjson.JSONObject cannot be cast to xxx
生活随笔
收集整理的這篇文章主要介紹了
解决问题 com.alibaba.fastjson.JSONObject cannot be cast to xxx
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
{"msg": "success","code": "success","data": [{"create_time": "2020-11-09 11:54:08","exec_status": "正常","host_id": 1527414,"id": 3136268,"ip": "10.127.22.132","modify_time": "2020-11-09 11:54:15","task_name": "dns插件執行","ts_id": 1600,"uuid": "1ca74de5-35f9-4f91-8cfb-7d4311eb8e45"},{"create_time": "2020-11-09 11:50:18","exec_status": "正常","host_id": 1527414,"id": 3136267,"ip": "10.127.22.132","modify_time": "2020-11-09 11:50:25","task_name": "dns插件執行","ts_id": 1599,"uuid": "1ca74de5-35f9-4f91-8cfb-7d4311eb8e45"}]
}
獲取到的json如上所示。
JSONObject object = JSONObject.parseObject(response);List<QlbSnatPluginExecute> qlbSnatPluginExecutes = (List<QlbSnatPluginExecute>) object.get("data");Map<String,List<QlbSnatPluginExecute>> map = qlbSnatPluginExecutes.stream().collect(Collectors.groupingBy(QlbSnatPluginExecute::getIp));這么寫在第三行代碼會報錯com.alibaba.fastjson.JSONObject cannot be cast to xxx
經過排查發現是嵌套的json處理問題,需要換一種處理方式。
JSONObject object = JSONObject.parseObject(response);List<QlbSnatPluginExecute> qlbSnatPluginExecutes = JSON.parseArray(object.getString("data"), QlbSnatPluginExecute.class);Map<String,List<QlbSnatPluginExecute>> map = qlbSnatPluginExecutes.stream().collect(Collectors.groupingBy(QlbSnatPluginExecute::getIp));第二行代碼中json轉自定義類的方式進行改變即可。
總結
以上是生活随笔為你收集整理的解决问题 com.alibaba.fastjson.JSONObject cannot be cast to xxx的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xml--Schema约束
- 下一篇: Leetcode--90. 子集Ⅱ