Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the data in a array from URL
    text
    copied!<p>I am trying to get data from a JSON URL. Of course by AsyncTask. But the problem is the JSON file does not have Object. It is an array. <a href="http://api.worldbank.org/countries/ir?format=json" rel="nofollow">JSON URL</a> and <a href="http://data.worldbank.org/node/11" rel="nofollow">api website</a> </p> <p>Can someone tell me how to create a JSONArray and JSONObject for this JSON file?!</p> <p>Here is what I have done which makes the app stop working</p> <pre><code>//URL to get JSON Array private static String url = "http://api.worldbank.org/countries/ir?format=json"; //JSON Node Names private static final String TAG_OBJ = "user"; private static final String NAME = "name"; private static final String CAPITALCITY = "capitalCity"; . . . class GetJSONTask extends AsyncTask&lt;String, Void, JSONObject&gt; { protected JSONObject doInBackground(String... urls) { // Creating new JSON Parser JSONParser jParser = new JSONParser(); // Getting JSON from URL JSONObject json = JSONParser.getJson(url); return json; } protected void onPostExecute(JSONObject json) { //Getting JSON Array try { user = json.getJSONArray(TAG_OBJ); JSONObject c = user.getJSONObject(0); //Stroing JSON item in a Variable String name = c.getString(NAME); String capitalCity = c.getString(CAPITALCITY); //Importing TextView final TextView view1 = (TextView)findViewById(R.id.name); final TextView view2 = (TextView)findViewById(R.id.capitalCity); //Set JSON Data in TextView view1.setText(name); view2.setText(capitalCity); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload