Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make an array when the JSON file does not have any and only got objects
    primarykey
    data
    text
    <p>I am trying to make an app that gets data from url on Android. I noticed that JSON files are different. Some of them start with an arrach name and some not. Those that have the array name <a href="http://api.learn2crack.com/android/json/" rel="nofollow">like this</a> which "user" is the array name work in my app but those that does not have any array name does not work. <a href="http://api.worldbank.org/countries/ir?format=json" rel="nofollow">For example this</a>.</p> <p>This is the code that I try to make array an object but it does not work for the second JSON file as it does not find the array name:</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_USER = "user"; private static final String TAG_ID = "id"; private static final String TAG_NAME = "name"; private static final String TAG_EMAIL = "email"; JSONArray user = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Creating new JSON Parser JSONParser jParser = new JSONParser(); // Getting JSON from URL JSONObject json = jParser.getJSONFromUrl(url); try { // Getting JSON Array user = json.getJSONArray(TAG_USER); JSONObject c = user.getJSONObject(0); // Storing JSON item in a Variable String id = c.getString(TAG_ID); String name = c.getString(TAG_NAME); String email = c.getString(TAG_EMAIL); //Importing TextView final TextView uid = (TextView)findViewById(R.id.uid); final TextView name1 = (TextView)findViewById(R.id.name); final TextView email1 = (TextView)findViewById(R.id.email); //Set JSON Data in TextView uid.setText(id); name1.setText(name); email1.setText(email); } catch (JSONException e) { e.printStackTrace(); } } </code></pre> <p>So this example is for the first JSON part. Can you tell me how to have similar code but for the second JSON file please?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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