0

The Class:

 public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        new Read().execute("masjids");

    }

    public JSONObject retrieveInfo(String radius) throws ClientProtocolException,
            IOException, JSONException {
        StringBuilder url = new StringBuilder(
                "http://iqamah.org/api/index.php?lat=43&long=-79&radius=");
        url.append(radius);

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet get = new HttpGet(url.toString());
        HttpResponse r = httpclient.execute(get);
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONObject timeline = new JSONObject(data);
        return timeline.getJSONObject("1");
    }

    private class Read extends AsyncTask<String, Integer, String> {

        ProgressDialog pd = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(MainActivity.this);
            pd.setTitle("Loading...");
            pd.setMessage("Please wait...");
            pd.setCancelable(false);
            pd.show();

        }

        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            try {
                JSONObject json = retrieveInfo("200");
                return json.getString(arg0[0]);
            } catch (ClientProtocolException 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 null;
        }

        @Override
        protected void onPostExecute(String status) {
            super.onPostExecute(status);
            pd.dismiss();

            TextView day = (TextView) findViewById(R.id.displayDay);
            day.setText("Masjid: " + status);

        }

    }

}

The JSON:

{
    "status": "ok",
    "masjids": [
        {
            "id": "44|Madina Masjid|1200 Danforth Ave, Toronto|49.99"
        },
        {
            "id": "39|Darul Taqwa|1 Thorncliffe Park Drive, Toronto|51.59"
        },
        {
            "id": "43|Darul Khair|25 St Dennis, Toronto|52.12"
        }
    ]
}

I am trying to access the masjids->id[0] array, how can I do this with the above code?

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
user2122179
  • 73
  • 1
  • 7
  • 1
    hmmm.. by reading JSonObject documents and how to use it in case of array. – Harry Joy Mar 01 '13 at 04:48
  • check this, i answered a similar question few days back, you can adapt the answer for your response as well; http://stackoverflow.com/questions/13576676/how-to-parse-complex-json-file-in-android/13576784#13576784 hope it helps. – Abhishek Nandi Mar 01 '13 at 05:48

3 Answers3

0
String data = "{ "status": "ok", "masjids": [ { "id": "44|Madina Masjid|1200 Danforth Ave, Toronto|49.99" }, { "id": "39|Darul Taqwa|1 Thorncliffe Park Drive, Toronto|51.59" }, { "id": "43|Darul Khair|25 St Dennis, Toronto|52.12" } ] }";

JSONObject jObj = new JSONObject(data);
JSONArray jArray_masjids = jObj.getJSONArray("masjids");
String address = jArray_masjids.getJSONObject(0).getString("id");
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
0
 JSONObject timeline = new JSONObject(data);
JasonArry jarry=timeline.getJsonArray("masjids");
for (int i = 0; i < jarry.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
String newStr = explrObject.getString("id");
}
kyogs
  • 6,766
  • 1
  • 34
  • 50
0

Try this,

String data = "{ "status": "ok", "masjids": [ { "id": "44|Madina Masjid|1200 Danforth Ave,     Toronto|49.99" }, { "id": "39|Darul Taqwa|1 Thorncliffe Park Drive, Toronto|51.59" }, { "id":    "43|Darul Khair|25 St Dennis, Toronto|52.12" } ] }";

 JSONObject jObj = new JSONObject(data);
 JSONArray jArray_masjids = jObj.getJSONArray("masjids");
 for(int i = 0 ;i<jArray_masjids;i++){
 String address = jsonArray.getJSONObject(i).getString("id");

 }
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22