org.json.JSONArray cannot be converted to JSONObject (Populating spinner from webservice)
android, json, spinner
Solution
as in Log :
org.json.JSONArray cannot be converted to JSONObject
because you are getting `JSONArray` as root element instead of `JSONObject` but you are trying to convert it to JSONObject. currently you are getting following json structure :
[ //<<<<<<<<< JSONArray
{ //<<<<<<<<< JSONObject
},
....
]
you will need to convert json string to `JSONArray` then extract `JSONObject` from it
JSONArray drinkJSON = bgt.execute().get();
you also need to change `doInBackground` method return type to `JSONArray`
Problem
I am getting a JSON exception when trying to parse JSON data im pulling off a web service. I have looked at relevant questions relating to this exception however I still cant come up with a solution. This is the Json array from the webservice ``` [ { "created_at":"2013-03-18T21:30:19Z", "id":1, "name":"latte", "price":"5", "updated_at":"2013-03-18T21:30:19Z" }, { "created_at":"2013-03-18T21:30:41Z", "id":11, "name":"black", "price":"2", "updated_at":"2013-03-18T21:30:41Z" }, { "created_at":"2013-03-19T09:38:31Z", "id":21, "name":"Tea", "price":"2", "updated_at":"2013-03-19T09:39:02Z" } ] ``` This is the relevant part of the class that uses the parser as a background task ``` public class SpinnerDemo extends Activity { //JSON node names private static final String TAG_DATA = "data"; private static final String TAG_CREATED_AT = "created_at"; private static final String TAG_ID_DRINK = "id"; private static final String TAG_NAME = "name"; private static final String TAG_PRICE = "price"; private static final String TAG_UPDATED_AT = "updated_at"; private static final String MAP_API_URL = "http://notTheActualURL.com/drinks.json"; private BackGroundTask bgt; Spinner drinkField; ArrayList<Drink> drinkList = new ArrayList<Drink>(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner_main); buildDrinkDropDown(); } private void buildDrinkDropDown() { List<NameValuePair> apiParams = new ArrayList<NameValuePair>(1); apiParams.add(new BasicNameValuePair("call", "drinkList")); bgt = new BackGroundTask(MAP_API_URL, "GET", apiParams); try { JSONObject drinkJSON = bgt.execute().get(); // Getting Array of drinks JSONArray drinks = drinkJSON.getJSONArray(TAG_DATA); // looping through All drinks for (int i = 0; i < drinks.length(); i++) { JSONObject d = drinks.getJSONObject(i); // Storing each json item in variable String id = d.getString(TAG_ID_DRINK); String createdAt = d.getString(TAG_CREATED_AT); String updatedAt = d.getString(TAG_UPDATED_AT); String price = d.getString(TAG_PRICE); String name = d.getString(TAG_NAME); // add drink drinkList.add(new Drink( createdAt ,id, name, price,updatedAt )); } ``` This is the actual class thats doing the parsing ``` package com.android.main; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import android.os.AsyncTask; import android.util.Log; public class BackGroundTask extends AsyncTask<String, String, JSONObject> { List<NameValuePair> postparams = new ArrayList<NameValuePair>(); String URL = null; String method = null; static InputStream is = null; static JSONObject jObj = null; static String json = ""; public BackGroundTask(String url, String method, List<NameValuePair> params) { this.URL = url; this.postparams = params; this.method = method; } @Override protected JSONObject doInBackground(String... params) { // TODO Auto-generated method stub // Making HTTP request try { // Making HTTP request // check for request method if (method.equals("POST")) { // request method is POST // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(URL); httpPost.setEntity(new UrlEncodedFormEntity(postparams)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } else if (method == "GET") { // request method is GET DefaultHttpClient httpClient = new DefaultHttpClient(); String paramString = URLEncodedUtils .format(postparams, "utf-8"); URL += "?" + paramString; HttpGet httpGet = new HttpGet(URL); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "utf-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data TEST " + e.toString()); } // return JSON String return jObj; } } ``` This is the exception from log cat ``` 03-26 11:32:49.637: E/JSON Parser(714): Error parsing data TEST org.json.JSONException: Value [{"id":1,"created_at":"2013-03-18T21:30:19Z","updated_at":"2013-03-18T21:30:19Z","price":"5","name":"latte"},{"id":11,"created_at":"2013-03-18T21:30:41Z","updated_at":"2013-03-18T21:30:41Z","price":"2","name":"black"},{"id":21,"created_at":"2013-03-19T09:38:31Z","updated_at":"2013-03-19T09:39:02Z","price":"2","name":"Tea"}] of type org.json.JSONArray cannot be converted to JSONObject ``` And another error showing a null point exception in the spinnerDemo class although i think this is a result of the previous exception ``` 03-26 11:32:49.707: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.main/com.android.main.SpinnerDemo}: java.lang.NullPointerException ``` Any advice or help with this would be greatly appreciated