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. JSON URL and api website
Can someone tell me how to create a JSONArray and JSONObject for this JSON file?!
Here is what I have done which makes the app stop working
//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<String, Void, JSONObject> {
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();
}
}
}