tape worm
tape worm

Reputation: 31

Array cannot be converted to JSONObject Android

I am trying to parse the JSON code in Android, while I am parsin I get the above error.

This is the JSON I want to parse:

 [{"value":"91.84","timestamp":"2012-10-11 14:12:13"}]

Here is the way I am parsing it:

InputStream inputStream = null;
String result = null;
HttpResponse response;
BufferedReader reader;
JSONObject jObject;
JSONArray jArray = null;
String aJsonString1;
String aJsonString2;

public class ReadJSON extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... url) {

        DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("url is written here");
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");
        try {
            response = httpclient.execute(httppost);      
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }



        jObject = new JSONObject(sb.substring(0));



        for (int i=0; i < jArray.length(); i++)
        {
            JSONObject oneObject = jArray.getJSONObject(i);
            // Pulling items from the array
            String oneObjectsItem = oneObject.getString("value");
            String oneObjectsItem2 = oneObject.getString("timestamp");
        }


        aJsonString1 = jObject.getString("value");
        aJsonString2 = jObject.getString("timestamp");

        return aJsonString1;

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("JSON",e.getMessage() + "  " + e);
        }           
        return null; // Error case

    }

    protected void onPostExecute(String result) {
        textView.setText(aJsonString1);

        if(aJsonString1==null){
            textView.setText("nothing to show");
        }

    }

}

So, can you see the problem here? What is wrong with it?

Upvotes: 1

Views: 2411

Answers (1)

rajeshwaran
rajeshwaran

Reputation: 1500

your json is JSONArray not JSONObject

change your code,

json has multiple JSONObject , then use following code

JSONArray jsonArray = new JSONArray(sb);

 for (int i=0; i < jsonArray.length(); i++)
    {
        JSONObject oneObject = jsonArray.getJSONObject(i);
        // Pulling items from the array
        String oneObjectsItem = oneObject.getString("value");
        String oneObjectsItem2 = oneObject.getString("timestamp");
    }

Suppose json has , single JSONObject

aJsonString1  = jsonArray.getJSONObject(0).getString("value");
aJsonString2  = jsonArray.getJSONObject(0).getString("timestamp");

Upvotes: 3

Related Questions