Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON数组循环仅获取和复制JSON数组的最后一个对象_Java_Android_Arrays_Json - Fatal编程技术网

Java JSON数组循环仅获取和复制JSON数组的最后一个对象

Java JSON数组循环仅获取和复制JSON数组的最后一个对象,java,android,arrays,json,Java,Android,Arrays,Json,我试图循环一个JSON对象数组,但我的循环只获取并复制JSON数组上的最后一个元素。有人能帮我吗 这是我从中获取JSON数组的JSON文件 { "data":[ { "sno":1, "id":"5", "title":"Position Yourself For Marriage2", "desc":"Position Yourself For Marriage", "free":"Yes", "image":"http

我试图循环一个JSON对象数组,但我的循环只获取并复制JSON数组上的最后一个元素。有人能帮我吗

这是我从中获取JSON数组的JSON文件

{  "data":[  
  {  
     "sno":1,
     "id":"5",
     "title":"Position Yourself For Marriage2",
     "desc":"Position Yourself For Marriage",
     "free":"Yes",
     "image":"http:\/\/app-web.moneyacademy.ng\/uploads\/5a815bc805fc9cb5a8a619dc43ead169.jpg",
     "date_added":"2017-10-17 05:48PM",
     "no_comment":0,
     "comments":0
  },
  {  
     "sno":2,
     "id":"4",
     "title":"Position Yourself For Marriage",
     "desc":"Position Yourself For Marriage",
     "free":"Yes",
     "image":"http:\/\/app-web.moneyacademy.ng\/uploads\/4209d035814c5ae5d1978ad1d85fc65b.jpg",
     "date_added":"2017-10-17 05:47PM",
     "no_comment":0,
     "comments":0
  },
  {  
     "sno":3,
     "id":"3",
     "title":"This Is Great Again",
     "desc":"The details of how a UUID is generated are determined by the device manufacturer and are specific to the device's platform or model.The details of...",
     "free":"Yes",
     "image":"http:\/\/app-web.moneyacademy.ng\/uploads\/145277f3d0499ee8e0dafbac384ca9b4.jpg",
     "date_added":"2017-10-12 10:26PM",
     "no_comment":3,
     "comments":[  
        {  
           "id_comment":"5",
           "id_user":"1",
           "id_nugget":"3",
           "comment":"Thank you all for your comment. We love you",
           "status":"1",
           "date_commented":"2017-10-16 00:25:18"
        },
        {  
           "id_comment":"1",
           "id_user":"1",
           "id_nugget":"3",
           "comment":"This is great and I love money academy",
           "status":"1",
           "date_commented":"2017-10-14 00:00:00"
        },
        {  
           "id_comment":"2",
           "id_user":"2",
           "id_nugget":"3",
           "comment":"This is a great work and I love it",
           "status":"1",
           "date_commented":"2017-10-14 00:00:00"
         }
       ]
     }
   ]
 }
这是我的代码,在这里我获取并循环通过JSON数组

    ArrayList<nauget> naugets = new ArrayList<>();

    try {

        JSONObject baseJsonResponse = new JSONObject(freeNaugetJson);
        JSONArray dataArray = baseJsonResponse.getJSONArray("data");

        // If there are results in the data array
        for (int i = 0; i <= dataArray.length(); i++){

            String title = dataArray.getJSONObject(i).getString("title");
            String body = dataArray.getJSONObject(i).getString("desc");
            String totalComments = dataArray.getJSONObject(i).getString("no_comment");
            String image = dataArray.getJSONObject(i).getString("image");

            ArrayList<Comment> comments = new ArrayList<>();

            //fetch each comment detail
            if (Integer.parseInt(totalComments) > 0) {
                JSONArray commentArray = dataArray.getJSONObject(i).getJSONArray("comments");

                for (int j = 0; j < commentArray.length(); j++) {
                    String userID = commentArray.getJSONObject(i).getString("id_user");
                    String userComment = commentArray.getJSONObject(i).getString("comment");

                    comments.add(new Comment(userID, userComment));
                }
            }

            Log.d("debugger", "Looped comments" + comments);
            // Create a new nauget object
            naugets.add(new nauget(title, body, image, totalComments, comments));
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Problem parsing the nauget JSON results", e);
    }

按以下方式更改您的状况:

for (int i = 0; i < dataArray.length(); i++)

试试这个改变你的状况

使用这个

for(int i = 0; i < dataArray.length(); i++)
for(int i = 0; i <= dataArray.length(); i++)

“i for(int j=0;jfor(int i = 0; i < dataArray.length(); i++)
for(int i = 0; i <= dataArray.length(); i++)
for (int i = 0; i <dataArray.length(); i++)
{

        String title = dataArray.getJSONObject(i).getString("title");
        String body = dataArray.getJSONObject(i).getString("desc");
        String totalComments = dataArray.getJSONObject(i).getString("no_comment");
        String image = dataArray.getJSONObject(i).getString("image");

        ArrayList<Comment> comments = new ArrayList<>();

        //fetch each comment detail
        if (Integer.parseInt(totalComments) > 0) 
        {
           JSONArray commentArray = dataArray.getJSONObject(i).getJSONArray("comments");

           for (int j = 0; j < commentArray.length(); j++) {

              JSONObject cmtObject = commentArray.getJSONObject(j);
              String userID = cmtObject.getString("id_user");
              String userComment = cmtObject.getString("comment");

              Log.i("ID", ":" + userID);
              Log.i("Comment", ":" + userComment);

              comments.add(new Comment(userID, userComment));
            }
        }

        Log.d("debugger", "Looped comments" + comments);
        // Create a new nauget object
        naugets.add(new nauget(title, body, image, totalComments, comments));
}
I/ID: :1
I/Comment: :Thank you all for your comment. We love you
I/ID: :1
I/Comment: :This is great and I love money academy
I/ID: :2
I/Comment: :This is a great work and I love it