Parsing complex JSON in Android

Guys i’m trying to parse this JSON below

{"message": "Success", 
"code": 2002, 
    {"article": [
                    {"timestamp_epoch": 1359967680, 
                    "search_score": 10.0, 
                    "headline": "When Sachin felt like bunking cricket practice", 
                    "timestamp": "2013-02-04 08:48:00", 
                    "related_article_count": 0, 
                    "excerpt": "...their campaign 'Support my School' by NDTV in Mumbai on Sunday. Sachin Tendulkar and Aishwarya Rai Bacchan during an event 3/5 Sachin Tendulkar and Aishwarya Rai Bacchan during an event India's Sachin Tendulkar with actress Aishwarya Rai Bachchan during the...", 
                    "source": {"name": "OneIndia", 
                                "url": "", 
                                "daylife_url": "", 
                                "rank": 3, 
                                "favicon_url": "", 
                                "source_id": "0e0Jd1Xczb7oW", 
                                "type": "MAINSTREAM"}, 
                    "url": "", 
                    "daylife_url": "", 

                    "article_id": "07nC5eTbBE0YA"}, 

                    {"timestamp_epoch": 1360361400, 
                    "search_score": 8.69279902947, 
                    "headline": "Sachin equals Sunny",

I’m just interested in headline and url values. I’m easily getting those values with this java code below.

JSONObject root = new JSONObject(result);
            JSONObject response = root.getJSONObject("response");

            String attributeId = response.getString("message");

            JSONObject payload = response.getJSONObject("payload");
            JSONArray article = payload.getJSONArray("article");

            for (int i = 0; i < article.length(); i++) {
                JSONObject c = article.getJSONObject(i);
                String headline = c.getString("headline");
                String url = c.getString("url");
            arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_1, strings);


Here i’m setting headline in ListView(this part is working fine). And my question here is : when i click any list it should pass corresponding url to the WebView activity(actually I’m implementing this in another class). So how to achieve this, how to store corresponding url with headline?? please someone help me..

Thanks in advance..


You can create a class named Article having two properties(String heading and String url). Then generate getter/setter for both the properties. now create a ArrayList of Article type like..

ArrayList<Article> articleList = new ArrayList<Article>();

Then while parsing your data do as..

Article article = new article();

and put value into object like..


and add object to arraylist like..


after all when click any list item, to hadle it add OnItemClickListener in your main activity. it will give you position of item clicked. then you can get corresponding object’s url from arcleList like..

articleList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            String url = articleList.get(position).getUrl();
            Intent i = new Intent(MainActivity.this,   WebViewActivity.class);
            i.putExtra("url", url);

now put this url into intent in main activity and receive it into webActivity and use it in webView finally…

Leave a Reply

Your email address will not be published. Required fields are marked *