这是调用返回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
我甚至不得到引用创建JSON对象中的值的点。这会在JSON对象本身的初始创建时炸弹。 – user3241191
请检查您已经硬编码的JSON字符串。这是无效的,你可以在http://jsonlint.com/ – jagmohan
检查它的有效性Json字符串是有效的,我已经使用json lint。如果它不是一个有效的字符串,那么硬编码的字符串就不会起作用。 – user3241191