简体   繁体   中英

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.

This the sample JSON content:

    [
  {

    "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)); Android Studio is red_highlighting imageURL and when I hover it I see: "Cannot resolve method 'imageURL(java.lang.String)'.

Please, what am I doing wrong?

Stacktrace from Implementaing Shadab Ansari Answer

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. 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);
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM