0

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
8
  • 1
    First validate your json in jsonlint.com Commented Oct 10, 2012 at 11:20
  • Because it is not valid json response. Do what hardik joshi suggested.i think \ is problem . Remove \ from your json response. Commented Oct 10, 2012 at 11:20
  • if you look that exception,u can see it is converted to a proper json.. Commented Oct 10, 2012 at 11:25
  • @vivek, the first string (the one with the backslashes), is that what you get from the server? Or have you escaped it to be a java string? Commented Oct 10, 2012 at 11:33
  • The server response seems weird even if you replace \" with " (and remove the surrounding quotes. Mane of the fields have double quotes. Even though JSON allows ' inside the strings, it is highly unlikely that this is what's intended. Did you write the server your self? That is where you need to start. Can you supply code for the server? Commented Oct 10, 2012 at 11:40

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I made changes to my answer, try it now.
i already tried this one..even tried to remove hidden characters..still not working
if u look that error description,it is saying "of type java.lang.String cannot be converted to JSONObject".But json string that appearing in error description is a valid json string.
I know. I understand and myself have validated the string you have mentioned. Do you have access to server?
is anything to change from server,then i need to contact my client
|
0

First convert your response to a String then try to create a JSONObject

1 Comment

if your objMngr.getResponse() returns a valid Json format String then there should be no problem in creating json object.......
0

I think, getResponse() is already string but response isn't valid JSON.If response isn't string,you can convert string with toString() method.

Comments

0

It seem there are some hidden characters on your string. Try this

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.