[英]How to get some elements in nested JSON objects in a JSON array
我有這個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",
},
},...
從json中,我只需要title ,主要對象的摘錄元素。 然后從featured_picture對象中,我只需要源元素。
我已編寫此代碼,但似乎無法正常工作:
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);
}
}
您將不會在這里繼續閱讀數組
for (int f = 0; f<array.length(); f++) {
featured_picture
是地圖中的一項,也返回地圖。
acces應該是這樣的:
array.getJSONObject(i).getJSONObject("featured_picture").getString("source");
你必須通過鑰匙識別JSON對象和數組,然后發現價值,一旦你學會了,然后JSON的復雜性沒有關系解析跟着教程
您的代碼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); }
是不正確的,json的這一部分不是數組,而是另一個jsonObject內部的對象。
嘗試以這種方式解析嵌套的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);
}
}
在這里,無需迭代循環即可讀取嵌套的JSONobject
。
因為“ featured_picture”僅提供JSONObject
而不提供數組。 如果它的返回數組應該是這樣的:
JSONObject rootObject=new JSONObject();
JSONArray nestedObject=rootObject.getJSONArray("key");
在這里,我以正確的方式修改了您的代碼,希望對您有所幫助。
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.