【问题标题】:ERROR converting from JSONArray to JSONObject processing Android错误从 JSONArray 转换为 JSONObject 处理 Android
【发布时间】: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


【解决方案1】:

我看到你正在手动解析 Json 字符串,你可以做到,而且速度足够快,但就我而言,我总是使用某种类型的库来为我完成这项工作,所以我想推荐你看看谷歌的 Gson 库。 https://code.google.com/p/google-gson/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多