我在读取一个JSON数组:
JSON语法文件的案例如下:
语法前面都有一个花括号。当我执行这些代码块时,系统会报错说:
A JSONArray text must start with '[' at character 1 of....
有谁遇见过这样的问题吗?能提供案例的代码块更好
<string name="json_array">
{"calendar":
{"calendarlist":
[
{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
]
}
} </string>
解析代码:
public void parseJsonArray(String string){
try {
JSONObject jsonObject = new JSONObject(string).getJSONObject("calendar");
JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
StringBuffer sb = new StringBuffer();
for(int i = 0;i < jsonArray.length();i ++){
JSONObject json = (JSONObject) jsonArray.opt(i);
String calendar_id = json.getString("calendar_id");
String title = json.getString("title");
String category_name = json.getString("category_name");
String showtime = json.getString("showtime");
String endshowtime = json.getString("endshowtime");
String allDay = json.getString("allDay");
sb.append("calendar_id: " + calendar_id + "\n\n" + "title: " + title + "\n\n" +
"category_name: " + category_name + "\n\n" + "showtime: " + showtime + "\n\n" +
"endshowtime: " + endshowtime + "\n\n" + "allDay: " + allDay + "\n\n" +
"-------------------------------------\n\n");
}
text.setText(sb.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。