0

Facing JSONException while parsing JSON String

Exception :

org.json.JSONException: Value anyType of type java.lang.String cannot be converted to JSONArray

Code snippet.

try {
   androidHttpTransport.call(Soap_Action1, envelope);
   SoapObject response = (SoapObject) envelope.getResponse();
   String resp=response.toString();

   Log.d("resp",response.toString());
   // newwwwww
   try {
       JSONArray jsonArray = new JSONArray(resp);
       for (int i = 0; i < jsonArray.length(); i++) {
           JSONObject c = jsonArray.getJSONObject(i);
           System.out.println(c.getInt("MST_BloodGroupID"));
           System.out.println(c.getString("BloodGroup_Name"));
       }
   } catch (JSONException e) {
       e.printStackTrace();
   }
}

response.toString() is below:

anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; element=anyType{}; }; }; }; }; }; }; }; diffgram=anyType{DocumentElement=anyType{Table=anyType{MST_BloodGroupID=1; BloodGroup_Name=A+; }; Table=anyType{MST_BloodGroupID=2; BloodGroup_Name=A-; }; Table=anyType{MST_BloodGroupID=3; BloodGroup_Name=B+; }; Table=anyType{MST_BloodGroupID=4; BloodGroup_Name=B-; }; Table=anyType{MST_BloodGroupID=5; BloodGroup_Name=AB+; }; Table=anyType{MST_BloodGroupID=6; BloodGroup_Name=AB-; }; Table=anyType{MST_BloodGroupID=7; BloodGroup_Name=O+; }; Table=anyType{MST_BloodGroupID=8; BloodGroup_Name=O-; }; }; }; }

asgs
  • 3,928
  • 6
  • 39
  • 54
sweety
  • 1
  • 2

3 Answers3

0

private class GetCategories extends AsyncTask {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Fetching..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("categories");                        

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        System.out.println(catObj.getInt("id"));
                        System.out.println(catObj.getString("name"));
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }
0

Your response.toString() is not returning data that is valid JSON. A quick way to check if your strings are ever valid JSON is to plug it into this site: http://jsonviewer.stack.hu/ If it's valid, you can switch over to the viewer tab and visualize your json to make sure your code is checking for JSONArrays and JSONObjects in relation to the brackets and curly braces correctly, most errors I've experienced with parsing JSON stem from me just misreading my dataset. The site will shout loudly at you if it's invalid, as it does with your data.

I'd recommend using the GSON library to create your JSON data. After importing it into your project, you can use it like:

Gson gson = new Gson();
SoapObject response = (SoapObject) envelope.getResponse();
String resp = gson.toJson(response);

Beyond that your approach to creating JSON objects and arrays from the string data seems to be correct.

Cogentleman
  • 321
  • 2
  • 5
0

You can't convert the String to JSONArray becayse the Strings isn't Array its an JSONObject.

try to convert the String to JSONObject the get the array from the JSONObject using it's key.

Basbous
  • 3,927
  • 4
  • 34
  • 62