如何解析嵌套的JSON对象数组

时间:2016-04-01 21:51:11

标签: java android json

首先,我知道已经提出了类似的问题。在所有问题中,我找到了一个对我有帮助的问题:https://stackoverflow.com/a/29798632/4293403但我在将自己接受的答案应用到我自己的场景时遇到了问题。

此示例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",

    },

  },...

这是代码:

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

    }

排队postItem.setPost_image(imageURL(ConfigPost.TAG_POST_IMAGE)); Android Studio是red_highlighting imageURL,当我悬停它时,我看到:“无法解析方法'imageURL(java.lang.String)'。

拜托,我做错了什么?

实施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

1 个答案:

答案 0 :(得分:0)

您的代码中没有任何此类方法 -

imageURL(String param)

我认为您只想设置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);
                }