php - 从 JSONArray 转换为 JSONObject 处理 Android 时出错

标签 php android mysql arrays json

我在尝试处理 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/

相关文章:

php - Godaddy 托管 HTTP GET 请求长度问题

android - 使用打盹模式在前台服务中禁用唤醒锁定 - Android M 中的新电池优化

主机窗口上的Android模拟器无法访问代理

Mysql从另一个表插入多个id

php - cakephp 使用 Between 进行高级过滤

php - 我一直在 PHP 代码中得到 "Error Querying Database"

php - 对象存储的创建方法

java - 超声波通讯

没有语法错误的 MySQL 错误 1064

mysql - Access 查询: Like statement containing a today method