我在尝试处理 JSON 时遇到了不断出现的错误。 Value [] of type org.json.JSONArray无法转换为JSONObject是我不断收到的错误,我不知道是什么原因造成的。 JSON 是:
{
"recipe":[
{
"recipeID":"1",
"recipeName":"chicken salad",
"ingredients":"chicken lettuce tomato cucumber"
},
{
"recipeID":"2",
"recipeName":"banana shake",
"ingredients":"banana ice cream vanilla "
}
]
}
我正在做的处理是:
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");
}
但我不断收到此错误:/
最佳答案
我看到你正在手动解析 Json 字符串,你可以做到,而且速度足够快,但就我而言,我总是使用某种类型的库来为我完成这项工作,所以我想推荐你看看谷歌的 Gson 库。 https://code.google.com/p/google-gson/
关于php - 从 JSONArray 转换为 JSONObject 处理 Android 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505959/