Search code examples
androidjsonjsonexception

JSONException: java.lang.String cannot be converted to JSONObject


I know this question has been asked a number of times but I didn't find any relative answer of my question.

I am trying to read json data from asset folder, but I'm getting following exception while getting

I searched number of stuffs but didn't help. Please give me any reference or hint.
Thanks in Advance.


Solution

  • Use

       String searchedTerm = jsonObject.getString(TAG_SEARCHEDTERM);
       JSONArray results = jsonObject.getJSONArray(TAG_RESULTS); 
    

    instead of

      JSONObject searchedTerm = jsonObject.getJSONObject(TAG_SEARCHEDTERM);
      JSONArray results = searchedTerm.getJSONArray(TAG_RESULTS);  
    

    because TAG_SEARCHEDTERM is key-value pair instead of JSONObject and you are trying to cast a String value to JsonObject.