老师如果JsonArray Key是空值 那要怎解析JSON数据?
老师如果JsonArray Key是空值 那要怎解析JSON数据?....比如这种JSON.
[{"account":"jack","date":"20160501","amount":1500,"type":0},{"account":"jack","date":"20160501","amount":6000,"type":0},{"account":"jack","date":"20160502","amount":3000,"type":1},{"account":"jack","date":"20160501","amount":20000,"type":0},{"account":"jack","date":"20160501","amount":1000,"type":1}]
而我的CODE是这样打...不过出不了数据
KmshopResult kmshopResult = new KmshopResult();
JSONObject jsonObject = new JSONObject(result);
List<KmshopResult.Kmpost> kmpostList = new ArrayList<>();
JSONArray kmshops = jsonObject.getJSONArray("null");
if(kmshops!=null && kmshops.length()>0){
for(int index = 0; index<kmshops.length();index++){
JSONObject kmshop = (JSONObject) kmshops.get(index);
String account = kmshop.getString("account");
KmshopResult.Kmpost kmshopItem = new KmshopResult.Kmpost();
kmshopItem.setmAccount(account);
kmpostList.add(kmshopItem);
}
kmshopResult.setmKmposts(kmpostList);
}
mTextView.setText(kmshopResult.toString());
错误是
2019-03-20 14:57:23.762 5274-5274/com.imooc.demo W/System.err: org.json.JSONException: Value [{"account":"jack","date":"20160501","amount":1500,"type":0},{"account":"jack","date":"20160501","amount":6000,"type":0},{"account":"jack","date":"20160502","amount":3000,"type":1},{"account":"jack","date":"20160501","amount":20000,"type":0},{"account":"jack","date":"20160501","amount":1000,"type":1}] of type org.json.JSONArray cannot be converted to JSONObject
2019-03-20 14:57:23.762 5274-5274/com.imooc.demo W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
正在回答
你可以创建一个JSONArray对象传入url,遍历获取的JSONArray对象,循环中使用JSONArray对象的getJSONObject(index)获取到每一组数据,塞到泛型集合中
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星