2012-10-10 125 views
0

在我的Android應用程序,我調用一個web服務,它是一個返回裝置jsonobject.In我得到這樣一個響應..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 } ] } ]}" 

我試圖將其轉換成這樣

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

將對象轉換它拋出一個異常,當「java.lang.String中不能被轉換爲JSONObject的」 ......下面是確切的例外,這是throwig ..What的原因,以及如何我可以解決這個問題嗎?

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

首先驗證您的json在http://jsonlint.com/ –

+0

因爲它不是有效的json響應。做什麼hardik joshi建議。我認爲\是問題。從你的json響應中刪除\。 –

+0

是的,它是有效的json .. – vmb

回答

0

首先轉換您的字符串響應,那麼嘗試創建一個的JSONObject

+0

可以共享該代碼... – vmb

+0

它已經是一個字符串。 –

+0

亞..它已經是一個字符串..objMngr。 getResponse() – vmb

1

嘗試

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

你jsonString似乎還好吧。但是,您的響應類型可能不是字符串。嘗試一下。 問題是服務器發送已經轉義的引號。

+0

它不是wor國王.. – vmb

+0

我對我的答案進行了更改,現在就試試。 – harshit

+0

我已經試過這一個..even試圖刪除隱藏的字符..still不工作 – vmb

0

我認爲,getResponse()已經是字符串,但響應無效JSON。如果響應不是字符串,可以使用toString()方法轉換字符串。

+0

沒有..那不管用 – vmb

0

看起來你的字符串上有一些隱藏的字符。 試試這個

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

好像你已經在服務器端傾銷對象JSON字符串的兩倍。

對象--dumps() - > JSON字符串--dumps() - >以JSON

一個字符串,那麼你應該刪除第二傾倒。

否則,你可以通過這種方式避開你的字符串How to unescape a Java string literal in Java?

我認爲第一種方法更好,更容易。

相關問題