Android:解析嵌套的 JSON 数组和 JSON 对象
Android: Parse the Nested JSON Array and JSON Object
我有来自 WordPress 站点的 JSON 内容
"posts": [
{
"id": 67986,
"type": "post",
"title": "Launching New eBooks",
"thumbnail_images": {
"full": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured.png",
"width": 700,
"height": 500
},
"medium": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured-300x214.png",
"width": 300,
"height": 214
},
}
}
我想从 medium
中获取 url
以显示为图像。在参考了一些SO问题后,我做了这段代码,并尝试进入循环。但不知何故我得到了整个 thumbnail_images
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONArray bigImage = object.getJSONArray("thumbnail_images");
for (int j = 0; j < bigImage.length(); j++) {
JSONObject tiObj = bigImage.getJSONObject(j);
JSONArray tiMed = tiObj.getJSONArray("medium");
for (int k = 0; k < tiMed.length(); k++) {
JSONObject tiMedU = tiMed.getJSONObject(i);
String imageURL = tiMedU.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
无法弄清楚上面的循环有什么问题。任何帮助都会很棒。谢谢
尝试使用这个
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONObject bigImage = object.getJSONObject("thumbnail_images");
JSONObject tiMed = bigImage.getJSONObject("medium");
String imageURL = tiMed.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
jsonarray.getJSONObject(i).
getJSONObject("thumbnail_images").
getJSONObject("medium").getString("url")
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++){
//JSONObject jsonObject1 = jsonArray.getJSONObject(i).getJSONObject("thumbnail_images");
System.out.println("apk----------------"+jsonArray.getJSONObject(i).getJSONObject("thumbnail_images").getJSONObject("medium").getString("url"));
}
response = CustomHttpClient.executeHttpGet("http://10.0.0.4:8000/login/?format=json&name="+username.getText().toString()+"&password="+pwd.getText().toString());
JSONArray jArray=new JSONArray(response);//Json Array
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);//Json Array To Json Object
Jenter code hereSONObject jsonObj2 = json_data.getJSONObject("fields");
usertype = jsonObj2.getString("usertype");
email = jsonObj2.getString("email");
}
Toast.makeText(getBaseContext(),email, Toast.LENGTH_SHORT).show();
我有来自 WordPress 站点的 JSON 内容
"posts": [
{
"id": 67986,
"type": "post",
"title": "Launching New eBooks",
"thumbnail_images": {
"full": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured.png",
"width": 700,
"height": 500
},
"medium": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured-300x214.png",
"width": 300,
"height": 214
},
}
}
我想从 medium
中获取 url
以显示为图像。在参考了一些SO问题后,我做了这段代码,并尝试进入循环。但不知何故我得到了整个 thumbnail_images
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONArray bigImage = object.getJSONArray("thumbnail_images");
for (int j = 0; j < bigImage.length(); j++) {
JSONObject tiObj = bigImage.getJSONObject(j);
JSONArray tiMed = tiObj.getJSONArray("medium");
for (int k = 0; k < tiMed.length(); k++) {
JSONObject tiMedU = tiMed.getJSONObject(i);
String imageURL = tiMedU.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
无法弄清楚上面的循环有什么问题。任何帮助都会很棒。谢谢
尝试使用这个
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONObject bigImage = object.getJSONObject("thumbnail_images");
JSONObject tiMed = bigImage.getJSONObject("medium");
String imageURL = tiMed.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
jsonarray.getJSONObject(i).
getJSONObject("thumbnail_images").
getJSONObject("medium").getString("url")
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++){
//JSONObject jsonObject1 = jsonArray.getJSONObject(i).getJSONObject("thumbnail_images");
System.out.println("apk----------------"+jsonArray.getJSONObject(i).getJSONObject("thumbnail_images").getJSONObject("medium").getString("url"));
}
response = CustomHttpClient.executeHttpGet("http://10.0.0.4:8000/login/?format=json&name="+username.getText().toString()+"&password="+pwd.getText().toString());
JSONArray jArray=new JSONArray(response);//Json Array
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);//Json Array To Json Object
Jenter code hereSONObject jsonObj2 = json_data.getJSONObject("fields");
usertype = jsonObj2.getString("usertype");
email = jsonObj2.getString("email");
}
Toast.makeText(getBaseContext(),email, Toast.LENGTH_SHORT).show();