简体   繁体   中英

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..

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

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

object --dumps()--> json string --dumps()-> one string in 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? .

The first way is better and easier i think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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