0

I'm trying to develop a json parser application. Here I'm getting a json parser exception error like "jsonarray cannot be converted to jsonobject"

I have following the below JSON format :

[
{
    "status": "ok",
    "data": [
        {
            "term_id": "28",
            "name": "All Meets",
        },
        {
            "term_id": "4",
            "name": "Classic Cars",
            },
         {
            "term_id": "30",
            "name": "Trucks",
           }
    ],
    "controller": "user1"
},
0
]

This is the code where the exception is thrown:

class GetCategories extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jsonParser.makeHttpRequest(url_getcategories, "POST", params);
        Log.d("Get Categories", json.toString());
        try {
            success = json.getString(TAG_SUCCESS); 
            if (success.equalsIgnoreCase("ok")) {
                JSONArray sys  = json.getJSONArray(TAG_CATEGORY);
                System.out.println("List of  Array =" + " " + sys); 
                for (int i = 0; i < sys.length(); i++) {
                    JSONObject c = sys.getJSONObject(i);
                    String cartitle = c.getString("name");
                     System.out.println("List of  Categories =" + " " + cartitle);   
                }          
            } else {
                error = json.getString(TAG_ERROR); 
                }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    } 

    @Override
    protected void onPostExecute(String file_url) {
        super.onPostExecute(file_url);
    }
}

From the above code what's wrong in the functionality? Please check it and provide a solution.

1
  • As in posted JSON String with question, root JSON object is JSONArray instead of JSONObject. so you will need to convert received string from server to JSONArray and also change return type of makeHttpRequest to JSONArray. Commented Sep 9, 2014 at 7:05

4 Answers 4

2
When i put your J son format in `http://www.jsoneditoronline.org/` , It gave me issue.

 So, i formatted it to 

   {
    "status": "ok",
    "data": [
        {
            "term_id": "28",
            "name": "All Meets"
        },
        {
            "term_id": "4",
            "name": "Classic Cars"
        },
         {
            "term_id": "30",
            "name": "Trucks"
         }
    ],
    "controller": "user1"
   } 

I think your code will work, with this format. You can refer Android JSON Parser Tutorial for more detailed solution for your issue.

Sign up to request clarification or add additional context in comments.

Comments

0

There's a difference between a JSON array and a JSON object. The former is contained in [] and the latter in {}.

You've got an array, and you're trying to create an object from it.

If the world were a sensible place, JSONObject and JSONArray would both descend from the same class, and you'd be able to instantiate and then ask it whether it's an array or an object. But sadly the world does silly things sometimes.

Comments

0

Your's is a JSON array [] and not a JSON object {}. Convert the string to a JSON Array first.

Comments

0

Try this way,hope this will help you to solve your problem.

class GetCategories extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONArray json = jsonParser.makeHttpRequest(url_getcategories, "POST", params);
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        try  {
            if (json.getJSONObject(0).getString(TAG_SUCCESS).equalsIgnoreCase("ok")) {
                JSONArray sys  = json.getJSONArray(TAG_CATEGORY);
                for (int i = 0; i < sys.length(); i++) {
                    HashMap<String,String> row = new HashMap<String, String>();
                    row.put("term_id",sys.getJSONObject(i).getString("term_id"));
                    row.put("name", sys.getJSONObject(i).getString("name"));
                    list.add(row);
                }
            }
            else{
                error = json.getJSONObject(0).getString(TAG_ERROR);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return list;

    }

    protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
        super.onPostExecute(result);

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.