0

I want to access posters and loop it in this JSON sting.

{"r":
    {"posters":
       [
        {"rev":10,"poster_id":4,"poster_filename":"545397373c2c7","poster_filepath":"\/poster_files\/545397373c2c7","poster_synopsis":"This is the new synopsis for this exquisite poster I have created. You will marvel at its greatness and ask many questions.","poster_title":"Poster A"},
        {"rev":6,"poster_id":7,"poster_filename":"5453b54b83b5f","poster_filepath":"\/poster_files\/5453b54b83b5f","poster_synopsis":"This is a synopsis that is not real. No one can say what this synopsis really means. It is total B.S..","poster_title":"Poster A"}
       ],
"msg":"72 & 2",
"status":"complete"}
}

I have this to convert the string to JSONObject:

JSONObject jsonObject = new JSONObject(result);         

JSONObject r = new JSONObject(jsonObject.getString("r"));

JSONArray posters = r.getJSONArray("posters");

Android Studio says "Array type expected, found org.json.JSONArray when I try to loop posters:

 int arraySize = posters.length();

 TextView myTextView = (TextView) findViewById(R.id.show_download_list);

 for(int i = 0; i < arraySize; i++) {
     myTextView.append(posters[i]);
     myTextView.append("\n");
 }

Am I not converting the object correctly??

1 Answer 1

1
JSONObject jsonObject = new JSONObject(result);         

JSONObject r = jsonObject.getJSONObject("r");

JSONArray posters = r.getJSONArray("posters");

I changed the r JSONObject to get it from the JSONObject called r instead of calling string called r ,, because r in the json string is an object

Update

Change first line in the loop to be myTextView.append(posters.getJSONObject(i).getString("poster_title")); , this will print the poster title on each text view.

  • the problem here is that you are dealing with the JSON array as an ordinary array.
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh...Thank YOU!! Is writing "new" not required when creating the object?
Yes new need to be used if you want to use the constructor. Please if the answer is correct mark as best answer to mark ur question as answered
strang, I am still getting the type error in the for loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.