1. Are you ready for the Galaxy S20? Here is everything we know so far!

JSON Array exception

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

  1. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter

    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
     


  2. Best Answer:
    Post #2 by Deleted User, Mar 7, 2016 (1 points)
  3. Deleted User

    Deleted User Guest

    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.
Loading...
Similar Threads - JSON Array exception
  1. beginnerProgrammer
    Replies:
    0
    Views:
    374
  2. Dinakar
    Replies:
    0
    Views:
    345
  3. anirudha
    Replies:
    0
    Views:
    289
  4. Rascalsailor
    Replies:
    2
    Views:
    373
  5. Chin Chun Seong
    Replies:
    0
    Views:
    333
  6. Korsholm
    Replies:
    0
    Views:
    233
  7. Jay Roa
    Replies:
    2
    Views:
    2,800
  8. mark9
    Replies:
    5
    Views:
    444
  9. darth_daniel
    Replies:
    9
    Views:
    1,105
  10. nagamothu
    Replies:
    30
    Views:
    1,640

Share This Page

Loading...