Looping through JSONObject to Create Array Java
<p>I have a <code>JSONObject</code> of the following format: </p>

<pre><code>[{"ascii_name":"Rio de Janeiro"},{"ascii_name":"Riyadh"},{"ascii_name":"Rome"},{"ascii_name":"Rawalpindi"},{"ascii_name":"Rabat"},{"ascii_name":"Recife"},{"ascii_name":"Ra's Bayrut"},{"ascii_name":"Rajkot"}]
</code></pre>

<p>which has been created using the following code: </p>

<pre><code>while($row = $all_cities-&gt;fetch_array())
{
 $myArray["ascii_name"] = $row["ascii_name"];
 $returnArray[] = $myArray;
}
echo json_encode($returnArray);
</code></pre>

<p>Now I am using a <code>Volley</code> task to load this into my Android app. In the <code>onResponse</code>method I want to obtain each of the city names and load it into an <code>ArrayList&lt;String&gt;</code> using  a <code>for</code> loop. This is my code now, however it won't work since <code>s</code> is a <code>JSONObject</code> not a <code>JSONArray</code>. </p>

<pre><code>@Override
        public void onResponse(String s) {
            try {
                    JSONObject jsonObject = new JSONObject(s);
                for (int i = 0; i &lt; jsonObject.length(); i++) {
                    JSONObject a = jsonObject.getJSONObject(i);
                    ....... more code which isn't relevant 
                }
</code></pre>