0

This is part of an AsyncTask that calls a web service that returns a JSON result. As you can see I hard coded the actual JSON return object. This was pulled directly from the error I got that specified it could not create the JSON object from the string object I was passing in, which is the variable result. That error occured when it hit new JSONObject(result) in the ParseResults method. Why would hard coding the exact string work but not the string being passed in?

@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 method snippet.

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

Also below, as i stated in a comment to another user, is the return statement that is returning the data. This is being returned from a .NET MVC application. I added in the UTF8 when that was mentioned and still get the same error.

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

And the entire error message:

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
user3241191
  • 505
  • 2
  • 5
  • 12

3 Answers3

1

type java.lang.String cannot be converted to JSONObject

This means "Use getString() for String"
getJSONObject() may cause this error.

class Response {
    String returnMessage;
    ...
}    

Response response;
response.returnMessage= "msg";

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

It maybe a encoding problem. Browser (and source editor) may have converted the result string encoding.

Q: ... I am storing items for the JSON data as Strings which is resulting in some odd character appearing A: new String(jo.getString("name").getBytes("ISO-8859-1"), "UTF-8");

Android JSON CharSet UTF-8 problems


Hard coded JSON string is valid. If you want to try, replace (\") with (") and paste it to the checker.

{
    "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 object is like a structure (or class)
It looks like this.

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

Sample code.

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();
    }
}
Community
  • 1
  • 1
Toris
  • 2,348
  • 13
  • 16
  • I don't even get to the point of referencing a value within the created JSON object. This bombs on the initial creation of the JSON object itself. – user3241191 Feb 21 '14 at 23:57
  • Please check the JSON string that you have hard coded. It is invalid, you can check for its validity at http://jsonlint.com/ – jagmohan Feb 22 '14 at 06:17
  • The Json string is valid and I already used json lint. If it wasn't a valid string, than the hard coded string wouldn't work which it does. – user3241191 Feb 22 '14 at 13:50
  • All the stuff you have there is really what I have in my ApiMethods class. All of the Api calls return a similare structure which is a ReturnMessage class that contains a return code, friendly message, another message for success or failure, a ReturnClass which is basically the class object being returned and than ReturnData is a list string that returns information that was passed in. So all the code you are showing I have built. As I said it works completely fine hard coded just not with the string being passed in which looks fine when I step through while debugging. – user3241191 Feb 22 '14 at 13:55
  • I even made sure to return the api call so it would use UTF-8 and still no go. `return Json(data: new { Response = returnValue }, contentType: "application/json", contentEncoding: System.Text.Encoding.UTF8, behavior: JsonRequestBehavior.AllowGet);` This is data returned from a .NET MVC application. – user3241191 Feb 22 '14 at 19:09
  • I see your error message. It means "Use getString() for String." If you use getJSONObject() for String, it causes the error. – Toris Feb 23 '14 at 06:45
  • How can I use the getJSONObject if my code is bombing when I am trying to create the initial JSONObject value? – user3241191 Mar 01 '14 at 22:02
1

Seems like your hardcoded json object is not a valid json object. This may be the reason why it throws exception. Check validitiy of json object here first.

Burak Dede
  • 3,725
  • 5
  • 40
  • 53
  • The result is valid, once you remove the \". That was done for escape character reasons within the string. I checked it with lint. If the hard coded result wasn't valid json than it wouldn't work, which it does. – user3241191 Feb 22 '14 at 13:50
0

Use this site to validate your json string

http://jsonlint.com/