I'm having difficult with an error I keep getting when trying to process my JSON. Value [] of type org.json.JSONArray cannot be converted to JSONObject is an error I keep getting and I don't know whats causing it. The JSON is:
{
"recipe":[
{
"recipeID":"1",
"recipeName":"chicken salad",
"ingredients":"chicken lettuce tomato cucumber"
},
{
"recipeID":"2",
"recipeName":"banana shake",
"ingredients":"banana ice cream vanilla "
}
]
}
And what I'm doing to process it is:
JSONArray recipes = null;
try {
JSONObject jsonObj = new JSONObject(json);
recipes = jsonObj.getJSONArray("recipe");
HashMap<String, String> map = new HashMap<String, String>();
// looping through All recipes
for (int i = 0; i < recipes.length(); i++) {
JSONObject c = recipes.getJSONObject(i);
name = c.getString("recipeName");
}
But I keep getting this error :/
I see you are parsing the Json string by hand, you can do it, and it is quickly enough, but in my case I always use some type of library that do the job for me, so I will like to recommend you that take a look at Gson library from google. https://code.google.com/p/google-gson/