简体   繁体   English

java.lang.String无法转换为JSONObject

[英]java.lang.String cannot be converted to JSONObject

In my android application,i calling one webservice and it is returning one jsonobject.In device i getting one response like this.. 在我的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    } ]  } ]}"

i trying convert it into an object like this 我试图将其转换为这样的对象

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

when converting it throwing one exception "java.lang.String cannot be converted to JSONObject"...below is the exact exception that it is throwig ..What is the reason and how can i solve this issue?? 转换时抛出一个异常“ java.lang.String无法转换为JSONObject” ...下面是确切的异常,它是throwig ..是什么原因,我如何解决这个问题?

 {  "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

try 尝试

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

Your jsonString seems allright. 您的jsonString似乎还可以。 However your response type may not be string. 但是,您的响应类型可能不是字符串。 Try it. 试试吧。 The problem is with already escaped inverted commas sent by the server. 问题在于服务器发送的转义逗号已经转义。

首先将您的响应转换为字符串,然后尝试创建JSONObject

我认为getResponse()已经是字符串,但是响应不是有效的JSON。如果响应不是字符串,则可以使用toString()方法转换字符串。

It seem there are some hidden characters on your string. 您的字符串上似乎有一些隐藏的字符。 Try this 尝试这个

return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));

Seems like you have dumped object to JSON string twice on server-side. 好像您在服务器端两次将对象转储到JSON字符串一样。

object --dumps()--> json string --dumps()-> one string in json 对象--dumps()-> json字符串--dumps()-> json中的一个字符串

So you should remove the second dumping. 因此,您应该删除第二个转储。

Otherwise you can unescape your string this way How to unescape a Java string literal in Java? 否则,您可以这种方式取消转义字符串。 如何取消Java中的Java字符串文字转义? .

The first way is better and easier i think. 我认为第一种方法更好,更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM