Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从JSONArray转换为JSONObject时出错_Php_Android_Mysql_Arrays_Json - Fatal编程技术网

Php 从JSONArray转换为JSONObject时出错

Php 从JSONArray转换为JSONObject时出错,php,android,mysql,arrays,json,Php,Android,Mysql,Arrays,Json,我在处理JSON时遇到了一个错误,这让我很难接受。 类型为org.json.JSONArray的值[]无法转换为JSONObject是我一直得到的错误,我不知道是什么导致了它。 JSON是: { "recipe":[ { "recipeID":"1", "recipeName":"chicken salad", "ingredients":"chicken lettuce tomato cucumber" },

我在处理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字符串,您可以这样做,而且速度足够快,但在我的情况下,我总是使用某种类型的库来为我完成这项工作,所以我想推荐您从google上查看Gson库。

在哪一行出现错误?json字符串是如何定义的?您正在使用JSONParser吗?@tinuviel Hi,androidstudio将错误指向这一行:JSONObject jsonObj=new JSONObjectjson@miselking json作为url请求的字符串从php返回我是android新手抱歉知识贫乏:@Lakhvir:意味着从服务器获取JSONArray而不是JSONObject