[英]How do I pull objects/array deep from a JSON parse in Android
JSON code: http://headytunes.co/?json=1 JSON代码: http : //headytunes.co/?json = 1
I am trying to pull the author name, but only repeating the first author in the JSON list. 我试图提取作者姓名,但只重复JSON列表中的第一位作者。 I am also trying to pull the thumbnail image and the multiple categories that come up from this JSON, but I can't correctly pull the image or the tags. 我也试图拉出缩略图和来自这个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);
You are only getting the first author name repeatedly because that is what you are doing in your code. 您只是重复获取第一个作者姓名,因为这是您在代码中所做的。
You currently have: 你目前有:
JSONObject jsonoTwo = jsono.getJSONArray("posts"). JSONObject jsonoTwo = jsono.getJSONArray(“posts”)。 getJSONObject(0) .getJSONObject("author"); getJSONObject(0) .getJSONObject(“author”);
. 。 . 。
actor.setAuthor(jsonoTwo.getString("name")); actor.setAuthor(jsonoTwo.getString( “名称”));
The bolded part above is saying to grab the data of the first post then grab the author. 上面的粗体部分是说抓住第一篇文章的数据然后抓住作者。
Update your code so that it looks like: 更新您的代码,使其看起来像:
actor.setAuthor(object.getJSONObject("author").getString("name"));
I hope it dose not have any typo : 我希望它没有任何拼写错误:
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);
You can simplify this whole process by looking at using Gson to create model classes from raw JSON. 您可以通过使用Gson从原始JSON创建模型类来简化整个过程。
To create a model class, simply have attributes of the same name as they are in the returned JSON, then parse your JSON into a list of models like such: 要创建模型类,只需使用与返回的JSON中相同名称的属性,然后将JSON解析为如下模型列表:
//Example model class
public Class Actor{
String name;
}
....
//Where you are parsing JSON
public void parseJSON(String json){
List<Actor> actors= Arrays.toList(new Gson().fromJson(json, Actor[].class));
//Use the actor object
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.