【发布时间】:2015-02-13 18:10:15
【问题描述】:
我在尝试处理我的 JSON 时不断遇到错误。 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”字符串是如何定义的?你在使用 JSONParser 吗?
-
@tinuviel 嗨,androidstudio 在这一行指出错误: JSONObject jsonObj = new JSONObject(json);
-
@miselking json 作为 url 请求中的字符串从 php 返回
-
@Lakhvir: 表示从服务器获取 JSONArray 而不是 JSONObject
标签: php android mysql arrays json