Home > Software engineering >  Difficult in fetching JSONObject data with two array name in RecyclerView -> JAVA (Android)
Difficult in fetching JSONObject data with two array name in RecyclerView -> JAVA (Android)

Time:01-04

I am trying to fetch data from an API in json format to display data in recycler view but couldn't fetch it. Some errors are showing and I am trying to fetch data. Please help or tell me where to solve. If any more details needed, I can provide more.

My JSON data:-

{

"data": [

{
"question_id": 1,
"sender_id": 27,
"receiver_id": 14,
"sender_time": "2022-12-30 06:11:28",
"question": "from user 30-12-2022",

"answer_details": [

{
"answer": "from ADMIN 30-12-2022",
"answer_id": 1,
"isAdmin": true
},

{
"answer": "from Users Again ",
"answer_id": 1,
"isAdmin": false
}
],
"sender_name": "Rahul",
"rating": 0,
"subject": "Study",
"subject_id": 0,
"receiver_name": "xcft Nth"
}
],
"message": "Chat getting successfully",
"status": 200
}

My Code:-

JSONArray dataArray = jsonObject.getJSONArray("data");

    for (int i = 0; i < dataArray.length(); i  ) {
    
    
    JSONObject o = new JSONObject(dataArray.get(i).toString());
    
    
    listOf_users_models.add(
    new ListOf_Users_Model(
    o.getInt("sender_id"),
    o.getInt("receiver_id"),
    o.getInt("question_id"),
    o.getString("sender_time"),
    o.getString("sender_name"),
    o.getString("subject"),
    o.getString("receiver_name"),
    o.getString("question")
    
    )
    );
    
    
    JSONArray dataArray2 = jsonObject.getJSONArray("answer_details"); //<- Error is here
    for (int ii = 0; ii < dataArray2.length(); ii  ) {
    
    Log.d("dataArray2",dataArray2.toString());
    
    
    
    JSONObject o2 = new JSONObject(dataArray2.get(ii).toString());
    
    listOf_users_models.add(
    new ListOf_Users_Model(
    
    o2.getString("answer"),
    o2.getInt("answer_id"),
    o2.getBoolean("isAdmin")
    
    
    )
    );
    
    }

My error message:- 
    
W/System: A resource failed to call close. 
W/System.err: org.json.JSONException: No value for answer_details
W/System.err:     at org.json.JSONObject.get(JSONObject.java:398)
W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:593)
    

CodePudding user response:

Here's where you went wrong.

W/System.err: org.json.JSONException: No value for answer_details

It means your JSON does not find any answer_details parameters.
you need to check your JSON Correctly and find cause.

  • If Your JSON Contains answer_details. it mean you've fetch data incorrect.

You need to declare and fetch data from JSON like this:

  1. in your code change like:
JSONArray dataArray2 = jsonObject.getJSONArray("answer_details")

to

JSONArray dataArray2 = o.getJSONArray("answer_details")

OR

JSONObject mainObject = new JSONObject("");
JSONArray dataArray = mainObject.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i  ) {
     JSONObject dataObject = dataArray.getJSONObject(i);
     String question_id = dataObject.getString("question_id");
     String sender_id = dataObject.getString("sender_id");
     String receiver_id = dataObject.getString("receiver_id");
     String sender_time = dataObject.getString("sender_time");
     String question = dataObject.getString("question");
     if (dataObject.has("answer_details")) {
         JSONArray answerArray = dataObject.getJSONArray("answer_details");
         for (int i1 = 0; i1 < answerArray.length(); i1  ) {
              JSONObject answerObject = dataArray.getJSONObject(i1);

              String answer = answerObject.getString("answer");
              String answer_id = answerObject.getString("answer_id");
              boolean isAdmin = answerObject.getBoolean("isAdmin");
         }
     }
     String sender_name = dataObject.getString("sender_name");
     String rating = dataObject.getString("rating");
     String subject = dataObject.getString("subject");
     String subject_id = dataObject.getString("subject_id");
     String receiver_name = dataObject.getString("receiver_name");
}
String message = mainObject.getString("message");
String status = mainObject.getString("status");
  • Related