2014-02-21 139 views
0

這是調用返回JSON結果的Web服務的AsyncTask的一部分。正如你所看到的,我硬編碼了實際的JSON返回對象。這是從我得到的錯誤直接拉,它指定它不能從我傳入的字符串對象,這是變量的結果創建JSON對象。當它在ParseResults方法中遇到new JSONObject(result)時發生該錯誤。爲什麼要硬編碼確切的字符串工作,而不是傳入的字符串?解析JSON webservice結果android

@Override 
     protected void onPostExecute(String result) { 

      try { 
       result = "{\"Response\":{\"ReturnCode\":200,\"ReturnMessage\":\"Information Successfully Retrieved\",\"ReturnData\":null,\"ReturnClass\":{\"PRO_ID\":\"11111111-1111-1111-1111-111111111111\",\"PRO_FirstName\":\"SILVER\",\"PRO_LastName\":\"HIYO\"},\"FriendlyErrorMessage\":null}}"; 
       JSONObject jsonObject = new ApiMethods().ParseResult(result); 

ParseResults方法片段。

public JSONObject ParseResult(String result) throws JSONException 
    { 
     JSONObject returnedObject = new JSONObject(); 
     try 
     { 
      JSONObject jsonObject = new JSONObject(result); 

同樣在下面,正如我在其他用戶的評論中所述,返回語句是返回數據。這是從.NET MVC應用程序返回的。當我提到UTF8時,我添加了它,但仍然出現相同的錯誤。

return Json(data: new { Response = returnValue }, contentType: "application/json", contentEncoding: System.Text.Encoding.UTF8, behavior: JsonRequestBehavior.AllowGet); 

和整個錯誤消息:

org.json.JSONException: Value {"Response":{"ReturnCode":200,"ReturnMessage":"Information Successfully Retrieved","ReturnData":null,"ReturnClass":{"PRO_ID":"11111111-1111-1111-1111-111111111111","PRO_FirstName":"Silver","PRO_LastName":"HIYO"},"FriendlyErrorMessage":null}} of type java.lang.String cannot be converted to JSONObject 

回答

1

java.lang.String類型不能轉換到的JSONObject

這意味着 「使用的getString()爲字符串」
getJSONObject()可能會導致此錯誤。

class Response { 
    String returnMessage; 
    ... 
}  

Response response; 
response.returnMessage= "msg"; 

JSONObjct obj; 
obj = response.getJSONObject("ReturnMessage"); // cannot be converted 

這可能是編碼的問題。瀏覽器(和源代碼編輯器)可能已經轉換了結果字符串編碼。

問:...我存儲用於JSON數據作爲字符串這是導致出現 一個一些奇怪的字符項:新的String(jo.getString(「名稱」)的getBytes(「ISO-8859 -1「),」UTF-8「);

Android JSON CharSet UTF-8 problems


硬編碼的JSON字符串是有效的。如果你想嘗試,用(「)替換(\」)並將其粘貼到檢查器。

{ 
    "Response": { 
     "ReturnCode": 200, 
     "ReturnMessage": "Information Successfully Retrieved", 
     "ReturnData": null, 
     "ReturnClass": { 
      "PRO_ID": "11111111-1111-1111-1111-111111111111", 
      "PRO_FirstName": "SILVER", 
      "PRO_LastName": "HIYO" 
     }, 
     "FriendlyErrorMessage": null 
    } 
} 

JSON對象是如下的結構(或類)
它看起來像這樣。

class Response { 
    int ReturnCode = 200; 
    String ReturnMessage = "Information Successfully Retrieved"; 
    ... 
} 

示例代碼。

protected void onPostExecute(String result) 
{ 
    JSONObject jsonObject; 
    JSONObject response; 
    int returnCode; 
    String returnMessage; 
    //JSONObject returnMessage; 

    result = "{\"Response\":{\"ReturnCode\":200,\"ReturnMessage\":\"Information Successfully Retrieved\",\"ReturnData\":null,\"ReturnClass\":{\"PRO_ID\":\"11111111-1111-1111-1111-111111111111\",\"PRO_FirstName\":\"SILVER\",\"PRO_LastName\":\"HIYO\"},\"FriendlyErrorMessage\":null}}"; 
    try 
    { 
     jsonObject = new JSONObject(result); 
     response = jsonObject.getJSONObject("Response"); 
     returnCode = response.getInt("ReturnCode"); 
     returnMessage = response.getString("ReturnMessage"); 
     //returnMessage = response.getJSONObject("ReturnMessage"); // This may cause same error 
    } 
    catch (JSONException e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

我甚至不得到引用創建JSON對象中的值的點。這會在JSON對象本身的初始創建時炸彈。 – user3241191

+0

請檢查您已經硬編碼的JSON字符串。這是無效的,你可以在http://jsonlint.com/ – jagmohan

+0

檢查它的有效性Json字符串是有效的,我已經使用json lint。如果它不是一個有效的字符串,那麼硬編碼的字符串就不會起作用。 – user3241191

1

看起來像你的硬編碼JSON對象不是一個有效的JSON對象。這可能是它拋出異常的原因。首先檢查json對象here的有效性。

+0

結果是有效的,一旦你刪除\「。這是爲了字符串內的轉義字符的原因,我用lint檢查過,如果硬編碼的結果不是有效的json比它不起作用, 。 – user3241191