Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Han
9,419 PointsHelp with JSONObject/JSONArray
I have a JSONObject of:
{
"page": 1,
"total_results": 380716,
"total_pages": 19036,
"results": [
{
"vote_count": 136,
"id": 335983,
"video": false,
"vote_average": 6.4,
"title": "Venom",
"popularity": 324.891,
"poster_path": "/2uNW4WbgBXL25BAbXGLnLqX71Sw.jpg",
"original_language": "en",
"original_title": "Venom",
"genre_ids": [
27,
878,
28,
53
],
"backdrop_path": "/VuukZLgaCrho2Ar8Scl9HtV3yD.jpg",
"adult": false,
"overview": "When Eddie Brock acquires the powers of a symbiote, he will have to release his alter-ego βVenomβ to save his life.",
"release_date": "2018-10-03"
},
I'm puzzled as to why I'm not getting anything from the code below:
List<Movie> movies = new ArrayList<>();
JSONObject results = new JSONObject(jsonData);
JSONArray array = new JSONArray(results.getJSONArray("results"));
JSONObject test = array.getJSONObject(0);
Log.d(TAG, test.getString("title"));
I assume I'm getting the JSONObject, then getting the JSONArray "results" and then the JSONObject at index 0, then getting the key "title".
Thanks!
1 Answer
kent1
5,437 PointsI can't test my answer right now, but I'm pretty sure you don't need to reconstruct the JSONArray returned from getJSONArray. So
JSONArray array = new JSONArray(results.getJSONArray("results"));
should just be
JSONArray array = results.getJSONArray("results");
James Han
9,419 PointsJames Han
9,419 PointsYup, figured it out after a while. Thanks!