【问题标题】:Android Studio error: org.json.JSONObject cannot be converted to JSONArrayAndroid Studio 报错:org.json.JSONObject 无法转换为 JSONArray
【发布时间】:2019-03-03 20:45:11
【问题描述】:

我正在尝试从生成用户名和密码的 api 获取 JSON 数据,但我没有这样做,请帮助

【问题讨论】:

  • 请将您的代码添加为文本而不是图像

标签: android arrays json


【解决方案1】:

您的 API 返回 JSON 对象而不是 JSONArray。直接在 JSONObject 而不是 JSONArray 中解析您的响应并从中检索 id 和密码。

JSONObject jsonObject= new JSONObject(response);
String id=jsonObject.getString("u_name");
String passworf=jsonObject.getString("u_pass");

【讨论】:

    【解决方案2】:

    您的 API 返回 JSON 对象而不是 JSONArray。

    检查这两者之间的区别: JSON Object vs JSONArray

    虽然您也可以使用 JSONObject,但只需执行以下操作:

    String value =  jsonObject.getString("key")
    

    如果您使用 Volley,则默认情况下 Volley 通过将其解析为 JSONObject 来返回成功响应。所以jsonObject 会被response 取代。

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多