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

Java 如何解析JSON对象的嵌套数组,java,android,json,Java,Android,Json,首先,我知道有人问过类似的问题。在所有的问题中,我发现了一个对我有帮助的问题:但是我在将公认的答案应用到我自己的场景中时遇到了问题 这是示例JSON内容: [ { "title": "This a Sample title for each post_title", "excerpt": "And this is a sample of the post_body", "featured_picture": { "source": "htt

首先,我知道有人问过类似的问题。在所有的问题中,我发现了一个对我有帮助的问题:但是我在将公认的答案应用到我自己的场景中时遇到了问题

这是示例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);
        }

    }
代码如下:

    [
  {

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

    }

您的代码中没有任何这样的方法-

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

对于(int f=0;fNot working Sir),应用程序崩溃,并在stacktrace47: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 W/system.Poster中显示MainActivity.onResponse处的系统错误(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)日志不完整。粘贴包含崩溃的完整logcat。这是其中的一部分,由于字符限制,无法粘贴其余部分。我已使用完整的stacktraceDid更新了问题。您调试了要传递给parseData()的数据?调试?不明白。