2014-02-21 140 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