1

I have a JSON string like the one below being returned from a REST app to an Android app:

"{ \"error\" : false , \"volume\" : 1000 }"

I then try to parse out the response with following (not the exact function, but the exact code causing trouble):

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public void jsonFunction(String result){
    JSONArray jsonArr = (JSONArray) new JSONParser().parse(result);
    JSONObject json = (JSONObject) jsonArr.get(0);
}

However, every time I try to parse the JSON string, I get the following error:

java.lang.String cannot be cast to org.json.simple.JSONArray
java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONArray

I've used the same code to parse a JSON response from another REST app within the same program, but that one executes without any trouble. If I try to parse directly to a JSONObject, I get a very similar error message. I figure there has to be something I'm not seeing, but for the life of me I can't quite seem to determine what it is.

EDIT:

This is what I get trying to parse directly to a JSON object:

java.lang.String cannot be cast to org.json.simple.JSONObject
java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONObject
2
  • 7
    The error is clear: The given JSON is not an array. Commented Jan 5, 2015 at 23:13
  • Fortunately I have control over the source, but even when I take the original data object, place it into an array, and then send the stringified response, I still get the same error. Commented Jan 6, 2015 at 15:03

1 Answer 1

4

Your json response is not an array, this is all you need to do.

  import org.json.JSONException;
  import org.json.JSONObject;

  try {
       JSONObject obj = new JSONObject(result);
       boolean error = obj.getBoolean("error");
       int volume = obj.getInt("volume");
  } catch (JSONException e){

  }
Sign up to request clarification or add additional context in comments.

9 Comments

org.json.simple apparently won't allow creating a JSON object in this fashion. It won't accept any parameters during the object declaration.
@MichaelMcCauley I just edited the answer to include the correct import of JSON you should be using.
Doing it this way, I get a similar error to above: Value {"error":false,"volume":1000} of type java.lang.String cannot be converted to JSONObject
Your json is probably not formatted correctly. "{ \"error\" : false , \"volume\" : 1000 }" Can I ask why you have the quotes escaped here?
This is the app on the other end, stringifying the response object prior to responding to the REST call. Believe it or not, but the app is written in JavaScript, and is not running in a browser. This seems to be what the JavaScript JSON engine employed by this server does when stringifying an object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.