• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps JSON Array exception

KarneeKarnay

Newbie
Dec 3, 2012
24
0
I'm working on an App that takes the data within a JSONArray and presents it in a list view to the user. The JSON data contains multiple sub-arrays. Below is an example.

Code:
{
error: "OK",
limit: 100,
offset: 0,
number_of_page_results: 1,
number_of_total_results: 1,
status_code: 1,
results: [
{
deck: "The first game in the Halo series of sci-fi first-person shooters. Halo: Combat Evolved is considered largely responsible for pioneering console shooters.",
id: 2600,
image: {
icon_url: "http://static.giantbomb.com/uploads/square_avatar/8/82063/2550125-haloce.jpg",
medium_url: "http://static.giantbomb.com/uploads/scale_medium/8/82063/2550125-haloce.jpg",
screen_url: "http://static.giantbomb.com/uploads/screen_medium/8/82063/2550125-haloce.jpg",
small_url: "http://static.giantbomb.com/uploads/scale_small/8/82063/2550125-haloce.jpg",
super_url: "http://static.giantbomb.com/uploads/scale_large/8/82063/2550125-haloce.jpg",
thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/8/82063/2550125-haloce.jpg",
tiny_url: "http://static.giantbomb.com/uploads/square_mini/8/82063/2550125-haloce.jpg"
},
name: "Halo: Combat Evolved",
resource_type: "game"
},

I have no problem getting into the "results" array and retrieving the "deck" & "name" JSONObjects. Now I want to access the image array and I'm getting an error a JSONException. Here's a copy of the code I'm using.

Java:
JSONObject oneObject = gameList.getJSONObject(i);
                getGameName(oneObject.getString("name"));
                getGameDeck(oneObject.getString("deck"));
                String temp = oneObject.getJSONArray("image").getJSONObject(0).getString("icon_url");

gameList is equal to a JSONArray containing all the "results". Not sure as to why this is causing an error. Should I treat image as a JSONObject or is it an Array? Here is a copy of the error.;

Code:
org.json.JSONException: Value {"icon_url":"http:\/\/static.giantbomb.com\/uploads\/square_avatar\/8\/82063\/2550125-haloce.jpg","medium_url":"http:\/\/static.giantbomb.com\/uploads\/scale_medium\/8\/82063\/2550125-haloce.jpg","screen_url":"http:\/\/static.giantbomb.com\/uploads\/screen_medium\/8\/82063\/2550125-haloce.jpg","small_url":"http:\/\/static.giantbomb.com\/uploads\/scale_small\/8\/82063\/2550125-haloce.jpg","super_url":"http:\/\/static.giantbomb.com\/uploads\/scale_large\/8\/82063\/2550125-haloce.jpg","thumb_url":"http:\/\/static.giantbomb.com\/uploads\/scale_avatar\/8\/82063\/2550125-haloce.jpg","tiny_url":"http:\/\/static.giantbomb.com\/uploads\/square_mini\/8\/82063\/2550125-haloce.jpg"} at image of type org.json.JSONObject cannot be converted to JSONArray
 
It's because the "image" element isn't an array.
Arrays in JSON are defined by using "[" "]"
image (below) is not an array, so oneObject.getJSONArray("image") throws an exception.

Code:
image: {
icon_url: "http://static.giantbomb.com/uploads/square_avatar/8/82063/2550125-haloce.jpg",
medium_url: "http://static.giantbomb.com/uploads/scale_medium/8/82063/2550125-haloce.jpg",
screen_url: "http://static.giantbomb.com/uploads/screen_medium/8/82063/2550125-haloce.jpg",
small_url: "http://static.giantbomb.com/uploads/scale_small/8/82063/2550125-haloce.jpg",
super_url: "http://static.giantbomb.com/uploads/scale_large/8/82063/2550125-haloce.jpg",
thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/8/82063/2550125-haloce.jpg",
tiny_url: "http://static.giantbomb.com/uploads/square_mini/8/82063/2550125-haloce.jpg"
},
 
  • Like
Reactions: KarneeKarnay
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones