public static List<News> getListNews(String urlPath) throws Exception {
final List<News> mlists = new ArrayList<News>();
final byte[] data = readParse(urlPath);
final JSONArray array = new JSONArray(new String(data));
for (int i = 0; i < array.length(); i++) {
final JSONObject item = array.getJSONObject(i);
final String id = item.getString("id");
final String title = item.getString("title");
final String content = item.getString("content");
final String outline = item.getString("outline");
mlists.add(new News(id,title,content,outline));
}
return mlists;
json数据是这样的
{"news":[{"content":"一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一","id":1,"outline":"11111111111111111111111","title":"111111111"},{"content":"二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二","id":2,"outline":"222222222222222222222222222222222222222","title":"222222222222"},{"content":"三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三","id":3,"outline":"333333333333333333333333333333333333333333333333333333333333333","title":"33333333333333"}]}
------解决思路----------------------
public static List<News> getListNews(String urlPath) throws Exception {
List<News> mlists = new ArrayList<News>();
byte[] data = readParse(urlPath);
JSONObject aJson = new JSONObject(new String(data));
JSONArray array = aJson.optJSONArray("news");
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.optJSONObject(i);
String id = item.optString("id");
String title = item.optString("title");
String content = item.optString("content");
String outline = item.optString("outline");
mlists.add(new News(id, title, content, outline));
}
return mlists;
}
这样行不行