JSONObject cannot be converted to JSONArray求救JSON:public static ArrayListHashMapString, Object
JSONObject cannot be converted to JSONArray求救
JSON:
public static ArrayList<HashMap<String, Object>> analysis(String jsonStr)
throws JSONException {
JSONArray jsonArray = null;
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject title = jsonArray.getJSONObject(i);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", title.getString("title"));
list.add(map);
}
return list;
}
这是报错异常:
W/System.err(8926): org.json.JSONException: Value {"total":10622,"ret":1,"blogs":[{"id":"34500","userName":"2014游全国","cover":"","title":"吃货最中意的冬季美食","cTime":"55分钟前","userId":"8159707"},{"id":"34499","userName":"2014游全国","cover":"","title":"上海美与不美","cTime":"58分钟前","userId":"8159707"},{"id":"34498","userName":"不孕不育医生","cover":"","title":"嫁给百分百男人PK恋战","cTime":"1小时前","userId":"5246320"},{"id":"34493","userName":"cat134","cover":"\/Public\/Uploads\/2014\/01\/07\/52cba951d42fb.jpg","title":"弘大一日游 love museum,特丽爱,Aland","cTime":"1小时前","userId":"8179322"},{"id":"34492","userName":"寻找轨迹","cover":"","title":"森林户外","cTime":"3小时前","userId":"8621148"},{"id":"34491","userName":"2NE-1","cover":"","title":"当你真正去等待一个人的时候,那个人永远都不会出现","cTime":"4小时前","userId":"1164275"},{"id":"34486","userName":"chinadive","cover":"\/Public\/Uploads\/2014\/01\/07\/52cb5fe2a4b4f.jpg","title":"水上的世界让我们引领你寻找最真实的味道(8264与三亚中仁联盟)","cTime":"7小时前","userId":"4687393"},{"id":"34484","userName":"费卢杰 FollowUp","cover":"http:\/\/www.517huwai.com\/Public\/Uploads\/2014\/01\/07\/52cae4a962d41.jpg","title":"三条栈-上太湖冰瀑一日穿越 By 谢光锋","cTime":"15小时前","userId":"3597900"},{"id":"34482","userName":"费卢杰 FollowUp","cover":"http:\/\/www.517huwai.com\/Public\/Uploads\/2014\/01\/06\/52cabf1adf8b4.jpg","title":"三道栈、冰瀑之旅 By 小桥流水","cTime":"16小时前","userId":"3597900"},{"id":"34480","userName":"精灵公子","cover":"\/Public\/Uploads\/2014\/01\/06\/52cab9ceb48da.jpg","title":"梦绕川藏线之 稻城—理塘—海子山—巴塘 (2013年9月6日)","cTime":"01-06 22:19","userId":"1785903"}]} of type org.json.JSONObject cannot be converted to JSONArray
01-07 17:08:03.093: W/System.err(8926): at org.json.JSON.typeMismatch(JSON.java:111)
01-07 17:08:03.093: W/System.err(8926): at org.json.JSONArray.<init>(JSONArray.java:91)
01-07 17:08:03.093: W/System.err(8926): at org.json.JSONArray.<init>(JSONArray.java:103)
01-07 17:08:03.093: W/System.err(8926): at com.huwai.JsonParse.JSONParse.analysis(JSONParse.java:50)
01-07 17:08:03.093: W/System.err(8926): at com.huwai.travle_517.TravleActivity$1.run(TravleActivity.java:43)
01-07 17:08:03.093: W/System.err(8926): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
01-07 17:08:03.093: W/System.err(8926): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-07 17:08:03.093: W/System.err(8926): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-07 17:08:03.093: W/System.err(8926): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-07 17:08:03.093: W/System.err(8926): at java.lang.Thread.run(Thread.java:841)
[解决办法]
jsonArray?=?new?JSONArray(jsonStr);?
这个改成
JSONObject jsonObj = new JSONObject(jsonStr);
一开始都是JSONObject,里面 blogs 就是JSONArray
jsonObj.getJSONArray("blogs ");