【问题标题】:JSONException: Value of type java.lang.String cannot be converted to JSONArrayJSONException:java.lang.String 类型的值无法转换为 JSONArray
【发布时间】:2014-12-19 06:31:47
【问题描述】:

我正在使用一些代码:

try {
       resultObject = new JSONObject(URLDecoder.decode(result, "UTF-8"));// no problem here
       resultJsonArray = resultObject.getJSONArray("data"); // error comes when run this line.

    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

这是整个错误信息:

org.json.JSONException: Value [{"first_name":"aig","phone_no":"3659428","passcode":"aig","last_name":"aig","user_id":"03343785-2714-43a5-a566-f4d9877ccafa","email_id":"aig.science@gmail.com"},{"first_name":"aig","phone_no":"635448448","passcode":"aig","last_name":"aig","user_id":"5dc26dcc-3f81-434a-b293-48438f2f920a","email_id":"aig.science@gmail.com"}] at data of type java.lang.String cannot be converted to JSONArray

格式化后(http://jsonformatter.curiousconcept.com/),JSON字符串会是这样的:

{  
   "response":"Success",
   "tablename":"USER_INFO",
   "transaction_type":"MODIFICATION_PULL_RESPONSE",
   "data":"[{\"first_name\":\"aig\",\"phone_no\":\"3659428\",\"passcode\":\"aig\",\"last_name\":\"aig\",\"user_id\":\"03343785-2714-43a5-a566-f4d9877ccafa\",\"email_id\":\"aig.science@gmail.com\"},{\"first_name\":\"aig\",\"phone_no\":\"635448448\",\"passcode\":\"aig\",\"last_name\":\"aig\",\"user_id\":\"5dc26dcc-3f81-434a-b293-48438f2f920a\",\"email_id\":\"aig.science@gmail.com\"}]"
}

【问题讨论】:

标签: java android json


【解决方案1】:

请避免""(双引号字符)在解析时通过异常直接导致JSON,始终使用\在这些字符之前,

字符串是零个或多个 Unicode 字符的序列,用双引号括起来,使用反斜杠转义。字符表示为单个字符串。字符串与 C 或 Java 字符串非常相似。 .它可以帮助你。

【讨论】:

  • 谢谢。实际上我在 Eclipse 中尝试了我的代码,它可以工作。但是当我尝试在 Android Studio 中这样做时,它没有。它是有线的。
【解决方案2】:

你做错了。试试下面的代码:

try {

   resultJsonArray = resultObject.getJSONArray(result);

} catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

【讨论】:

  • 嗨,MunimJi,谢谢。我试过你的代码,同样的错误。
  • 检查我编辑的答案。我觉得应该是这样的。
  • 嗨,MunimJi,谢谢。请检查我刚刚发布的解决方案。它不知道它为什么起作用。干杯。
【解决方案3】:
After testing for a while, I solved it in a wired way:

**Before:**

       try {
          resultObject = new JSONObject(URLDecoder.decode(result, "UTF-8"));// no problem here
          resultJsonArray = resultObject.getJSONArray("data"); // error comes when run this line.

       } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
       }


**After**

      try {
          resultObject = new JSONObject(URLDecoder.decode(result, "UTF-8"));// no problem here
          resultJsonArray = new JSONArray(resultObject.get("data").toString()); // Works!!!

       } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
       }

它确实是有线的。我不知道为什么会这样。

【讨论】:

  • h.. 的接线方式
  • 我遇到了同样的问题!您的解决方案 resultJsonArray = new JSONArray(resultObject.get("data").toString());正在为我工​​作。有人知道这里发生了什么吗?无论如何,谢谢你的解决方法,顺便说一句:我想你的意思是“奇怪”而不是“有线”
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多