1

I am new to JSON ,I have following JSON String which i am getting from a server want to use each object item and traverse this JSON.

No need to use GSON or Jackson Library. Video Id and thumb is most prior for me

     {'videos': [ { "video":                                                        {"duration":"2:51","views":36824,"video_id":"41141","rating":"4.25","ratings":"51","title":"                            video1","url":"http:\/\/www.xyz.com\/41141","default_thumb":"http:\/\/img02.xyz.com\/_thumbs   \/0000041\/0041141\/0041141_015m.jpg","thumb":"http:\/\/img02.xyz.com\/_thumbs\/0000041\/004      1141\/0041141_015m.jpg","publish_date":"2014-03-27 05:38:01"}},
  {"video":{"duration":"2:51","views":36825,"video_id":"4141","rating":"4.25","ratings":"51","title":"video2","url":"http:\/\/www.xyz.com\/4141","default_thumb":"http:\/\/img03.xyz.com\/_thumbs\/0000041\/0041141\/0041141_015m.jpg","thumb":"http:\/\/img03.xyz.com\/_thumbs\/0000041\/0041141\/0041141_015m.jpg","publish_date":"2014-03-27 05:38:01"}},
{ "video":{"duration":"2:51","views":36225,"video_id":"41412","rating":"4.25","ratings":"51","title":"video3","url":"http:\/\/www.xyz.com\/41412","default_thumb":"http:\/\/img04.xyz.com\/_thumbs\/0000041\/0041141\/0041141_015m.jpg","thumb":"http:\/\/img04.xyz.com\/_thumbs\/0000041\/004 1141\/0041141_016m.jpg","publish_date":"2014-03-27 05:38:01"}}
}}],"count":279369}
  • 1
    Did you already search on your own? http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – stevecross Mar 27 '14 at 10:26

3 Answers3

2
JSONArray videojarray   =jobj.getJSONArray("videos");
JSONObject videoJObject=null;
for(int j=0;j<videojarray.length();j++){
  videoJObject=videojarray.getJSONObject(j);
  JSONObject  videoJObj=videoJObject.getJSONObject("video");
  String videoid=videoJObj.getString("video_id");
  String thumb=videoJObj.getString("thumb");
}
raju
  • 785
  • 3
  • 14
  • 28
0

Your JSON is not valid, change single quote ' to double quote " at the video on start of JSON, and remove two closing bracket } before count. Then try to parse your valid JSON.

A corrected JSON is the bellow:

{
    "videos": [
        {
            "video": {
                "duration": "2:51",
                "views": 36824,
                "video_id": "41141",
                "rating": "4.25",
                "ratings": "51",
                "title": "video1",
                "url": "http://www.xyz.com/41141",
                "default_thumb": "http://img02.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "thumb": "http://img02.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "publish_date": "2014-03-27 05:38:01"
            }
        },
        {
            "video": {
                "duration": "2:51",
                "views": 36825,
                "video_id": "4141",
                "rating": "4.25",
                "ratings": "51",
                "title": "video2",
                "url": "http://www.xyz.com/4141",
                "default_thumb": "http://img03.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "thumb": "http://img03.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "publish_date": "2014-03-27 05:38:01"
            }
        },
        {
            "video": {
                "duration": "2:51",
                "views": 36225,
                "video_id": "41412",
                "rating": "4.25",
                "ratings": "51",
                "title": "video3",
                "url": "http://www.xyz.com/41412",
                "default_thumb": "http://img04.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "thumb": "http://img04.xyz.com/_thumbs/0000041/0041141/0041141_016m.jpg",
                "publish_date": "2014-03-27 05:38:01"
            }
        }
    ],
    "count": 279369
} 
miltos
  • 1,009
  • 1
  • 6
  • 10
0

Try this..

JSONObject jobj = new JSONObject(response);
JSONArray videosjarray = jobj.getJSONArray("videos");
for(int j=0;j < videosjarray.length();j++){
  JSONObject videosJObject = videosjarray.getJSONObject(j);
  JSONObject video = videosJObject.getJSONObject("video");
  String videoid = video.getString("video_id");
  String thumb = video.getString("thumb");
}

Your JSON is not valid check it from this like http://jsonlint.com/

Hariharan
  • 24,741
  • 6
  • 50
  • 54