Я программирую приложение, которое загружает новость на домашнюю страницу. Я использую JSON-файл для хранения всех моих данных. Моя проблема в том, что я не могу вытащить «комментарии» массива из внешнего «массива» внешнего массива. я даже запустить свой JSON через несколько валидаторов без проблем, однако при запуске приложения, я получаю это сообщение:Получите массив JSON внутри другого массива
System.err: org.json.JSONException: No value for comments
Вот моя JSON:
{
"feed": [
{
"id": 1,
"name": "National Geographic Channel",
"image": "http://api.androidhive.info/feed/img/cosmos.jpg",
"status": "\"Science is a beautiful and emotional human endeavor,\" says Brannon Braga, executive producer and director. \"And Cosmos is all about making science an experience.\"",
"profilePic": "http://api.androidhive.info/feed/img/nat.jpg",
"timeStamp": "1403375851930",
"url": null,
"comments": [
{
"comment": {
"id": 1,
"content": "Great Post xD",
"user": "SparkWings"
}
}
]
},
{
"id": 2,
"name": "TIME",
"image": "http://api.androidhive.info/feed/img/time_best.jpg",
"status": "30 years of Cirque du Soleil's best photos",
"profilePic": "http://api.androidhive.info/feed/img/time.png",
"timeStamp": "1403375851930",
"url": "http://ti.me/1qW8MLB",
"comments": [
{
"comment": {
"id": 1,
"content": "Great Post xD",
"user": "SparkWings"
}
}
]
},
{
"id": 3,
"name": "Abraham Lincoln",
"image": null,
"status": "That some achieve great success, is proof to all that others can achieve it as well",
"profilePic": "http://api.androidhive.info/feed/img/lincoln.jpg",
"timeStamp": "1403375851930",
"url": null,
"comments": [
{
"comment": {
"id": 1,
"content": "Great Post xD",
"user": "SparkWings"
}
}
]
}
]
}
А вот метод я m, пытающихся использовать для захвата всех данных.
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("feed");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));
item.setName(feedObj.getString("name"));
// Image might be null sometimes
String image = feedObj.isNull("image") ? null : feedObj
.getString("image");
item.setImge(image);
item.setStatus(feedObj.getString("status"));
item.setProfilePic(feedObj.getString("profilePic"));
item.setTimeStamp(feedObj.getString("timeStamp"));
JSONArray comments = response.getJSONArray("comments");
final ArrayList<String> comment = new ArrayList<>();
for(int j = 0; j < comments.length(); j++)
{
JSONObject o = (JSONObject) comments.get(i);
StringBuilder b = new StringBuilder();
int commentId = comments.getJSONObject(i).getInt("id");
String cont = comments.getJSONObject(i).optString("content");
String user = comments.getJSONObject(i).optString("user");
b.append(cont + " -");
b.append(user);
comment.add(b.toString());
}
item.setComments(comment);
// url might be null sometimes
String feedUrl = feedObj.isNull("url") ? null : feedObj
.getString("url");
item.setUrl(feedUrl);
feedItems.add(item);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
Любая помощь приветствуется. Заранее спасибо!
'response' ->' feedObj', закрытие как опечатка. – njzk2