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
4

5 回答 5

1

尝试

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

你的 jsonString 似乎没问题。但是,您的响应类型可能不是字符串。试试看。问题在于服务器发送的已经转义的逗号。

于 2012-10-10T12:23:51.943 回答
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.

于 2012-10-10T12:41:54.480 回答
0

好像您在服务器端两次将对象转储到 JSON 字符串。

object --dumps()--> json 字符串 --dumps()-> json 中的一个字符串

所以你应该删除第二次倾销。

否则,您可以通过这种方式对字符串进行转义 如何在 Java 中对 Java 字符串文字进行转义?.

我认为第一种方法更好,更容易。

于 2013-06-04T05:14:11.737 回答
0

您的字符串上似乎有一些隐藏字符。尝试这个

return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
于 2013-03-18T04:42:00.543 回答
0

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

于 2012-10-10T11:35:42.540 回答