0

I have a query related to JSON parsing data from URL.

Here is my scenario:

I have one URL like URL1 from where I get the JSON data.

there is data like this.

 {
  "Eat & Drink":{
    "Alfresco":3,
    "Bars":3,
    "Brewery":5,
    "Cafe":0,
    "Distillery":0,
    "Family":5, 
    "Fine Dining":0, 
    "Home Delivery":0,
    "Nightclubs":2,
    "Restaurant":0,
    "TakeAway":0,
    "Wineries":0}
}

I want to get the Key and Value fields without passing it because here it's are different at all the places.

So first I get the KEY like First one "Alfresco" and for using that key we get the value of that key Like "3".

0

2 Answers 2

2
package com.example.json_parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class SECOND extends Activity {
    /** Called when the activity is first created. */
     ArrayList<String> listItems ;
     ListView lv ;
     Button btn;
     EditText et;
     Bundle bunn;
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.lv);


        lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1, this.fetchTwitterPublicTimeline()));
    }
    public ArrayList<String> fetchTwitterPublicTimeline()
    {
        listItems = new ArrayList<String>();
        try {
            URL twitter = new URL(
                    "MENTION YOUR URL HERE");

            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);
                for (int i = 0; i < ja.length(); i++) {
                    try{
                        JSONObject jo = (JSONObject) ja.get(i);
                        //listItems.add(jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH")+"\n"+jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH")+"\n"+jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH"));
                        listItems.add(jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH")+"\n"+jo.getString("0"));
                    }catch(Exception e){

                    }


                }
            }


            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),listItems.get(arg2).toString() , Toast.LENGTH_SHORT).show();

                }
            });



        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listItems;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer but i think there is some problem i need to get "NODE ITEM" Key name so i don't need to pass it manually like when i loop array i get "Alfresco", "Bars", "Brewery", "Cafe", "Distillery" etc which are keys for the data and from that key i get the value for that key.
In my case i was having json as ["row",{"thoughtNo":"3","date":"03\/04\/2005","goldenQuote":"There are no bad employees, only bad managers."} .... and "Node Item" as i mentioned above was like "thoughtNo", "date" and etc..
1

Try below code :

    try {
        JSONObject jsonObject =new JSONObject(response);
        Iterator keys = jsonObject.keys();
        while(keys.hasNext()) {
            String key = (String) keys.next();
            Log.d("MainActivity", "key : " + key + "    name : " + jsonObject.optString(key));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.