[英]How to get some elements in nested JSON objects in a JSON array
I hava this JSON: 我有这个JSON:
[
{
"title": "This a Sample title for each post_title",
"excerpt": "And this is a sample of the post_body,
"author": "King Spark",
"featured_picture": {
"source": "https://exapmple.com/blah/blah/image.jpg",
"year": "2015",
"ownwer": "Akim Man",
},
},...
From the json I only need the title , excerpt elements of the main objects. 从json中,我只需要title ,主要对象的摘录元素。 Then from the featured_picture objects, I want only the source element. 然后从featured_picture对象中,我只需要源元素。
I have written this code and it seems not to be working: 我已编写此代码,但似乎无法正常工作:
private void parseData(JSONArray array){
Log.d(TAG, "Parsing array");
for(int i = 0; i<array.length(); i++) {
PostItems postItem = new PostItems();
JSONObject jsonObject = null;
try {
jsonObject = array.getJSONObject(i);
postItem.setPost_title(jsonObject.getString(ConfigPost.TAG_POST_TITLE));
postItem.setPost_body(jsonObject.getString(ConfigPost.TAG_POST_BODY));
//Parsing featured_pocture object
for (int f = 0; f<array.length(); f++) {
JSONObject object = array.getJSONObject(f);
JSONObject postImage = object.getJSONObject("featured_picture");
String imageURL = postImage.getString("source");
postItem.setPost_image(imageURL);
}
} catch (JSONException w) {
w.printStackTrace();
//Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show();
}
mPostItemsList.add(postItem);
}
}
You won't continue read the array here 您将不会在这里继续阅读数组
for (int f = 0; f<array.length(); f++) {
featured_picture
is an entry in the map and returns a map too. featured_picture
是地图中的一项,也返回地图。
The acces should be like this : acces应该是这样的:
array.getJSONObject(i).getJSONObject("featured_picture").getString("source");
你必须通过钥匙识别JSON对象和数组,然后发现价值,一旦你学会了,然后JSON的复杂性没有关系解析跟着教程
your code for (int f = 0; f<array.length(); f++) { JSONObject object = array.getJSONObject(f); JSONObject postImage = object.getJSONObject("featured_picture"); String imageURL = postImage.getString("source"); postItem.setPost_image(imageURL); }
您的代码for (int f = 0; f<array.length(); f++) { JSONObject object = array.getJSONObject(f); JSONObject postImage = object.getJSONObject("featured_picture"); String imageURL = postImage.getString("source"); postItem.setPost_image(imageURL); }
for (int f = 0; f<array.length(); f++) { JSONObject object = array.getJSONObject(f); JSONObject postImage = object.getJSONObject("featured_picture"); String imageURL = postImage.getString("source"); postItem.setPost_image(imageURL); }
for (int f = 0; f<array.length(); f++) { JSONObject object = array.getJSONObject(f); JSONObject postImage = object.getJSONObject("featured_picture"); String imageURL = postImage.getString("source"); postItem.setPost_image(imageURL); }
is not correct, this part of json is not an array but an object inside another jsonObject. for (int f = 0; f<array.length(); f++) { JSONObject object = array.getJSONObject(f); JSONObject postImage = object.getJSONObject("featured_picture"); String imageURL = postImage.getString("source"); postItem.setPost_image(imageURL); }
是不正确的,json的这一部分不是数组,而是另一个jsonObject内部的对象。
Try to parse the nested JSON like this way: 尝试以这种方式解析嵌套的JSON:
private void parseData(JSONArray array){
Log.d(TAG, "Parsing array");
for(int i = 0; i<array.length(); i++) {
PostItems postItem = new PostItems();
JSONObject jsonObject = null;
try {
jsonObject = array.getJSONObject(i);
postItem.setPost_title(jsonObject.getString(ConfigPost.TAG_POST_TITLE));
postItem.setPost_body(jsonObject.getString(ConfigPost.TAG_POST_BODY));
//Parsing featured_picture object
JSONObject postImage = jsonObject.getJSONObject("featured_picture");
postItem.setPost_image(postImage.getString("source"));
} catch (JSONException w) {
w.printStackTrace();
//Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show();
}
mPostItemsList.add(postItem);
}
}
Here no need to iterate loop to read nested JSONobject
. 在这里,无需迭代循环即可读取嵌套的JSONobject
。
Because "featured_picture" gives only JSONObject
not an array. 因为“ featured_picture”仅提供JSONObject
而不提供数组。 In case if its return array you should read like this: 如果它的返回数组应该是这样的:
JSONObject rootObject=new JSONObject();
JSONArray nestedObject=rootObject.getJSONArray("key");
Here i modified your code in correct manner hope will it help for you. 在这里,我以正确的方式修改了您的代码,希望对您有所帮助。
for(int i = 0; i<array.length(); i++) {
PostItems postItem = new PostItems();
JSONObject jsonObject = null;
try {
jsonObject = array.getJSONObject(i);
postItem.setPost_title(jsonObject.getString(ConfigPost.TAG_POST_TITLE));
postItem.setPost_body(jsonObject.getString(ConfigPost.TAG_POST_BODY));
//Parsing featured_pocture object
JSONObject postImage = jsonObject.getJSONObject("featured_picture");
String imageURL = postImage.getString("source");
postItem.setPost_image(imageURL);
} catch (JSONException w) {
w.printStackTrace();
//Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.