【发布时间】: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\"}]"
}
【问题讨论】:
-
在
JSONArray或JSONObject开始之前不能有任何String或Space,即root必须是这样的[或'{'。 -
@jww 在我提出问题之前,我检查了所有这些线程。在尝试了这些解决方案之后,不知何故我的问题仍然存在。
-
@Nitin Misra 我使用 jsonformatter.curiousconcept.com 检查了我的 json 字符串。它是有效的 json 字符串。
-
嘿,朋友们。我在 Eclipse 中尝试了我的代码,它们运行良好。但它们在 Android Studio 中不起作用。任何想法将不胜感激。谢谢