JSON 代码:http://headytunes.co/?json=1
我正在尝试提取作者姓名,但只重复 JSON 列表中的第一作者。我也在尝试从这个 JSON 中提取缩略图和多个类别,但我无法正确提取图像或标签。
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
Actors actor = new Actors();
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("posts");
JSONObject jsonoTwo =jsono.getJSONArray("posts").getJSONObject(0).getJSONObject("author");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("excerpt"));
actor.setDate(object.getString("date"));
actor.setAuthor(jsonoTwo.getString("name"));
//.setTags(jsonoThree.getString("title"));
//actor.setImage(images.getString("url"));
songList.add(actor);
最佳答案
我希望它没有任何错别字:
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("excerpt"));
actor.setDate(object.getString("date"));
actor.setAuthor(object.getString("name"));
JSONArray tags = object.getJSONArray("tag");
for (int j = 0; j < tags.length(); j++) {
// you can access each tag in this section
}
JSONArray attachment = object.getJSONArray("attachment");
for (int k = 0; k < tags.length(); k++) {
// you can access inside attachment
JSONObject insideAttachmentObj = attachment.getJSONObject(k);
insideAttachmentObj.getJSONObject("images").getJSONObject("thumbnail");
//or for accessing url :insideAttachmentObj.getJSONObject("images").getJSONObject("thumbnail").getString("url");
}
songList.add(actor);
关于java - 如何从 Android 中的 JSON 解析中深入提取对象/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276742/