Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

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.

Upvotes: 1

Views: 4960

Answers (2)

Pratik
Pratik

Reputation: 1541

I guess you need to get the Dish Name where you are getting exception. You can get the dish name shown below...

String searchedTerm = jsonObject.getString(TAG_SEARCHEDTERM);

Using this

JSONArray results = jsonObject.getJSONArray(TAG_RESULTS);

you will get the "results" JSONArray as shown in your json file.

and you can iterate through it using for loop.

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

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.

Upvotes: 4

Related Questions