Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this json url which outputs this (snippet)

{
   "status":true,
   "result":{
      "message":"Successfully retrieved the daily feed.",
      "items":{
         "1376438400":[
            {
               "code":"DjCr3N3o",
               "slug":"soulja-boy-gets-kicked-off-airplane",
               "cdn_screenshot_path":"screenshots\/2013\/08\/DjCr3N3o.png",
               "title":"Soulja Boy Gets Kicked Off Airplane!",
               "hits":"457",
               "date_added":"1376507797"
            },
            {
               "code":"7V9eOVpX",
               "slug":"dr.-dre-and-suge-knight-baby-mama-michelle-surprise-performance-she-sounds-like-a-chipmunk-but-sings-like-an-angel",
               "cdn_screenshot_path":"screenshots\/2013\/08\/7V9eOVpX.png",
               "title":"Dr. Dre AND Suge Knight Baby Mama Michel'le Surprise Performance! (She Sounds Like A Chipmunk But Sings Like An Angel!)",
               "hits":"525",
               "date_added":"1376505010"
            },

            {
               "code":"8ovO203r",
               "slug":"headless-snake-bites-itself-in-the-butt",
               "cdn_screenshot_path":"screenshots\/2013\/08\/8ovO203r.png",
               "title":"Headless Snake Bites Itself In The Butt!",
               "hits":"361",
               "date_added":"1376485812"
            }
         ],
         "1376352000":[
            {
               "code":"9b9jR6qs",
               "slug":"show-you-how-to-do-this-son-chris-paul-hits-4-straight-jumpers-on-colleges-best-point-guards",
               "cdn_screenshot_path":"screenshots\/2013\/08\/9b9jR6qs.jpg",
               "title":"Show You How To Do This Son! Chris Paul Hits 4 Straight Jumpers On College's BEST Point Guards!",
               "hits":"979",
               "date_added":"1376443810"
            },
            {
               "code":"p6l5pwg8",
               "slug":"ttbnez-fck-da-opp-music-video",
               "cdn_screenshot_path":"screenshots\/2013\/08\/p6l5pwg8.png",
               "title":"TTBNEZ - F*ck Da Opp [Music Video]",
               "hits":"316",
               "date_added":"1376419812"
            },
            {
               "code":"haxUoUVt",
               "slug":"strip-life-the-reality-series-feat.-lanipop-entyce-trailer",
               "cdn_screenshot_path":"screenshots\/2013\/08\/haxUoUVt.png",
               "title":"Strip Life: The Reality Series (feat. Lanipop, Entyce) [Trailer]",
               "hits":"426",
               "date_added":"1376419214"
            }
         ],

The problem I am having is figuring out how to parse it due to its format and how to reach the data such as "code", "slug" and "title". This is what I have so far, but it seems wrong as I may have to have 2 loops instead of 1 I think.

This is what I have so far

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array 
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrive JSON Objects from the given website URL in JSONfunctions.class
        jsonobject = JSONfunctions
                .getJSONfromURL("http://api.hoodplug.com/v1/videos/daily_feed?per_page=5&offset=0&format=json");

        try {
            // Locate the array name
            jsonarray = jsonobject.getJSONArray("item");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("code", jsonobject.getString("code"));
                map.put("slug", jsonobject.getString("slug"));
                map.put("title", jsonobject.getString("title"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
share|improve this question
    
jsonarray = jsonobject.getJSONArray("item"); the Array name should be "items" judging by your JSON... –  Tonithy Aug 15 '13 at 4:44

1 Answer 1

You're just missing a little point here!

in here you got the first json object :

 jsonobject = JSONfunctions
                .getJSONfromURL("http://api.hoodplug.com/v1/videos/daily_feed?per_page=5&offset=0&format=json");

this json object is the whole big JSON object that contain status and result but you directly want to access the JSON array that inside result object! using this :

// Locate the array name
jsonarray = jsonobject.getJSONArray("items");

The right thing is you must get the result object first from the big json object with

JSONObject resultObject = jsonobject.getJSONObject("result");

then use the resultObject to get the array!

try {
            // Locate the array name
            jsonarray = resultObject .getJSONArray("item");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("code", jsonobject.getString("code"));
                map.put("slug", jsonobject.getString("slug"));
                map.put("title", jsonobject.getString("title"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

I hope you understand my answer, but if you have other question about my answer feel free to ask! :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.