1. Download our Official Android App: Forums for Android!

Apps JSON Array exception

Discussion in 'Android Development' started by KarneeKarnay, Mar 7, 2016.

  1. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    24
    Joined:
    Dec 3, 2012

    Dec 3, 2012
    24
    0
    15
    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 (Text):
    1. {
    2. error: "OK",
    3. limit: 100,
    4. offset: 0,
    5. number_of_page_results: 1,
    6. number_of_total_results: 1,
    7. status_code: 1,
    8. results: [
    9. {
    10. 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.",
    11. id: 2600,
    12. image: {
    13. icon_url: "http://static.giantbomb.com/uploads/square_avatar/8/82063/2550125-haloce.jpg",
    14. medium_url: "http://static.giantbomb.com/uploads/scale_medium/8/82063/2550125-haloce.jpg",
    15. screen_url: "http://static.giantbomb.com/uploads/screen_medium/8/82063/2550125-haloce.jpg",
    16. small_url: "http://static.giantbomb.com/uploads/scale_small/8/82063/2550125-haloce.jpg",
    17. super_url: "http://static.giantbomb.com/uploads/scale_large/8/82063/2550125-haloce.jpg",
    18. thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/8/82063/2550125-haloce.jpg",
    19. tiny_url: "http://static.giantbomb.com/uploads/square_mini/8/82063/2550125-haloce.jpg"
    20. },
    21. name: "Halo: Combat Evolved",
    22. resource_type: "game"
    23. },
    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.

    Code (Java):
    1. JSONObject oneObject = gameList.getJSONObject(i);
    2.                 getGameName(oneObject.getString("name"));
    3.                 getGameDeck(oneObject.getString("deck"));
    4.                 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 (Text):
    1. 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
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    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 (Text):
    1.  
    2. image: {
    3. icon_url: "http://static.giantbomb.com/uploads/square_avatar/8/82063/2550125-haloce.jpg",
    4. medium_url: "http://static.giantbomb.com/uploads/scale_medium/8/82063/2550125-haloce.jpg",
    5. screen_url: "http://static.giantbomb.com/uploads/screen_medium/8/82063/2550125-haloce.jpg",
    6. small_url: "http://static.giantbomb.com/uploads/scale_small/8/82063/2550125-haloce.jpg",
    7. super_url: "http://static.giantbomb.com/uploads/scale_large/8/82063/2550125-haloce.jpg",
    8. thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/8/82063/2550125-haloce.jpg",
    9. tiny_url: "http://static.giantbomb.com/uploads/square_mini/8/82063/2550125-haloce.jpg"
    10. },
    11.  
     
    KarneeKarnay likes this.

Share This Page

Loading...