gpt4 book ai didi

java - JSONException : Value of type java. lang.String 无法转换为 JSONArray

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:24 25 4
gpt4 key购买 nike

我正在使用一些代码:

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\"}]"
}

最佳答案

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

字符串是零个或多个 Unicode 字符的序列,用双引号括起来,并使用反斜杠转义。一个字符被表示为单个字符串。字符串非常类似于 C 或 Java 字符串。.也许对你有帮助。

关于java - JSONException : Value of type java. lang.String 无法转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27560822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com