[英]How to Parse Nested arrays of JSON Objects
To start with, I know that similar questions have been asked. 首先,我知道有人问过类似的问题。 Among all the questions, I found one that was of help to me: https://stackoverflow.com/a/29798632/4293403 but I am having problem applying the accepted answer to my own scenerio. 在所有问题中,我发现了一个对我有帮助的问题: https : //stackoverflow.com/a/29798632/4293403,但我在将接受的答案应用于自己的场景时遇到了问题。
This the sample JSON content: 这是示例JSON内容:
[
{
"title": "This a Sample title for each post_title",
"excerpt": "And this is a sample of the post_body",
"featured_picture": {
"source": "https://exapmple.com/blah/blah/image.jpg",
"year": "2015",
"ownwer": "Akim Man",
},
},...
And this is the code: 这是代码:
//This method will parse json data
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_image 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(ConfigPost.TAG_POST_IMAGE));
} catch (JSONException w) {
w.printStackTrace();
//Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show();
}
mPostItemsList.add(postItem);
}
}
In line postItem.setPost_image(imageURL(ConfigPost.TAG_POST_IMAGE));
在postItem.setPost_image(imageURL(ConfigPost.TAG_POST_IMAGE));
Android Studio is red_highlighting imageURL
and when I hover it I see: "Cannot resolve method 'imageURL(java.lang.String)'. Android Studio是red_highlighting imageURL
,当我将其悬停时,我看到:“无法解析方法'imageURL(java.lang.String)'。
Please, what am I doing wrong? 拜托,我在做什么错?
Stacktrace from Implementaing Shadab Ansari Answer 实施Shadab Ansari的Stacktrace答案
04-01 23:47:51.372 747-775/? D/StatusBarManagerService: manageDisableList userId=0 what=0x0 pkg=Window{74ca6f8 u0 com.example.Poster/com.example.Poster.MainActivity}
04-01 23:47:51.372 17672-17672/com.example.Poster D/MainActivity: Parsing array
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.parseData(MainActivity.java:153)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity.access$000(MainActivity.java:43)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:118)
04-01 23:47:51.382 17672-17672/com.example.Poster W/System.err: at com.example.Poster.MainActivity$2.onResponse(MainActivity.java:109)
04-01 23:47:57.708 747-798/? W/InputDispatcher: channel '74ca6f8 com.example.Poster/com.example.Poster.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
04-01 23:47:57.708 747-798/? E/InputDispatcher: channel '74ca6f8 com.example.Poster/com.example.Poster.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-01 23:47:57.708 747-823/? W/InputDispatcher: Attempted to unregister already unregistered input channel '74ca6f8 com.example.Poster/com.example.Poster.MainActivity (server)'
04-01 23:47:57.708 747-823/? I/WindowState: WIN DEATH: Window{74ca6f8 u0 com.example.Poster/com.example.Poster.MainActivity}
04-01 23:47:57.718 747-1385/? W/ActivityManager: Force removing ActivityRecord{ded20d6 u0 com.example.Poster/.MainActivity t19141}: app died, no saved state
You don't have any such method in your code - 您的代码中没有任何此类方法-
imageURL(String param)
I think you want to set imageURL
only. 我认为您只想设置imageURL
。 In that case your code should look like - 在这种情况下,您的代码应类似于-
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.