java.lang.String 无法转换为 JSONObject

标签 java android json

在我的 Android 应用程序中,我调用一个 Web 服务,它返回一个 jsonobject。在设备中,我得到这样的一个响应..

"{  \"Time_Stamp\" : \"10/10/2012 4:26 PM\",  \"records\" : [ {    \"'Name'\" : \"'LD-00000002'\",    \"'Appointment_Date_Time'\" : \"'null'\",    \"'Phone'\" : \"'9909955555'\",    \"'Home_Country_Address'\" : \"'null'\",    \"'Occupation'\" : \"'null'\",    \"'SR_Appointment_Status'\" : \"'Open'\",    \"'Id'\" : \"'a0OE0000001iLynMAE'\",    \"'SR_Appointment_Comment'\" : \"'testing'\",    \"'ProductsOfInterest'\" : \"'null'\",    \"'ActivityName'\" : \"'Sales'\",    \"documentsList\" : [ ]  }, {    \"'Name'\" : \"'LD-00000002'\",    \"'Appointment_Date_Time'\" : \"'null'\",    \"'Phone'\" : \"'9909955555'\",    \"'Home_Country_Address'\" : \"'null'\",    \"'Occupation'\" : \"'null'\",    \"'SR_Appointment_Status'\" : \"'Open'\",    \"'Id'\" : \"'a0OE0000001iLynMAE'\",    \"'SR_Appointment_Comment'\" : \"'testing'\",    \"'ProductsOfInterest'\" : \"'null'\",    \"'ActivityName'\" : \"'Sales'\",    \"documentsList\" : [ {      \"numberOfImages\" : 3,      \"Name\" : \"new document\",      \"Mandatory\" : false,      \"FilePath\" : null,      \"Category\" : null    } ]  } ]}"

我尝试将其转换为这样的对象

  JSONObject jsonObj=new JSONObject(objMngr.getResponse());

在转换它时抛出一个异常“java.lang.String无法转换为JSONObject”...下面是它抛出的确切异常..原因是什么以及如何解决这个问题??

 {  "Time_Stamp" : "10/10/2012 4:26 PM",  "records" : [ {    "'Name'" : "'LD-00000002'",    "'Appointment_Date_Time'" : "'null'",    "'Phone'" : "'9909955555'",    "'Home_Country_Address'" : "'null'",    "'Occupation'" : "'null'",    "'SR_Appointment_Status'" : "'Open'",    "'Id'" : "'a0OE0000001iLynMAE'",    "'SR_Appointment_Comment'" : "'testing'",    "'ProductsOfInterest'" : "'null'",    "'ActivityName'" : "'Sales'",    "documentsList" : [ ]  }, {    "'Name'" : "'LD-00000002'",    "'Appointment_Date_Time'" : "'null'",    "'Phone'" : "'9909955555'",    "'Home_Country_Address'" : "'null'",    "'Occupation'" : "'null'",    "'SR_Appointment_Status'" : "'Open'",    "'Id'" : "'a0OE0000001iLynMAE'",    "'SR_Appointment_Comment'" : "'testing'",    "'ProductsOfInterest'" : "'null'",    "'ActivityName'" : "'Sales'",    "documentsList" : [ {      "numberOfImages" : 3,      "Name" : "new document",      "Mandatory" : false,      "FilePath" : null,      "Category" : null    } ]  } ]} of type java.lang.String cannot be converted to JSONObject

最佳答案

尝试

  JSONObject jsonObj=new JSONObject(objMngr.getResponse().toString().replace("\\", " "));

你的 jsonString 看起来没问题。但是您的响应类型可能不是字符串。尝试一下。 问题在于服务器发送的已转义的引号。

关于java.lang.String 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818073/

相关文章:

android - 在android中输入数据的自定义布局

android - Android 上的 AS3 Air - 加载 SWF 作为它自己的 Main()

javascript - JSON.parse 在代码中不起作用,但在开发人员控制台中工作正常

ios - Codable 对象映射数组元素到字符串

java - 如何使用@Step注释在Allure Report中打印长字符串?

java - 当我使用 Mockito 测试 get 方法时,是否超出了 get 方法的范围?

java - += 运算符在 Java 中是线程安全的吗?

java - "AWT-EventQueue-1"java.lang.OutOfMemoryError : Java heap space

java - 正则表达式按顺序查找单词但在其他单词之间

javascript - 在嵌套的 json 数据 Angular js 的情况下,如何为 ng-options 设置默认值?