5

I have a two dimensional JSON array object like below

{"enrollment_response":{"condition":"Good","extra":"Nothig","userid":"526398"}} 

I would like to parse the above Json array object to get the condition, extra, userid.So i have used below code

JSONParser parser = new JSONParser();

    try {

        Object obj = parser.parse(new FileReader("D:\\document(2).json"));

        JSONObject jsonObject = (JSONObject) obj;

        String name = (String) jsonObject.get("enrollment_response");
        System.out.println("Condition:" + name);

        String name1 = (String) jsonObject.get("extra");
        System.out.println("extra: " + name1);



    } catch (FileNotFoundException e) { 
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

Its throwing an error as

"Exception in thread "main" java.lang.ClassCastException:
org.json.simple.JSONObject cannot be cast to java.lang.String at
com.jsonparser.apps.JsonParsing1.main(JsonParsing1.java:22)"

Please anyone help on this issue.

5
  • What json lib do you use? Commented Feb 18, 2014 at 9:26
  • 2
    I think the error message is pretty clear. What result are you hoping to get by casting an object to a string (you do realize that the value of enrollment_response is an object?)? Commented Feb 18, 2014 at 9:28
  • 1
    What you have here is not an array at all. It is a Json object with another Json object nested in. Commented Feb 18, 2014 at 9:30
  • I would like to get the values of condition,extra and userid Commented Feb 18, 2014 at 9:31
  • FYI org.json.simple is quite possibly the worst JSON parsing library you could possibly choose. Please, please use something else. Jackson, Gson, or even the old json.org libs are far better. Commented Feb 18, 2014 at 9:54

3 Answers 3

3

First of all: Do not use the JSON parsing library you're using. It's horrible. No, really, horrible. It's an old, crufty thing that lightly wraps a Java rawtype Hashmap. It can't even handle a JSON array as the root structure.

Use Jackson, Gson, or even the old json.org library.

That said, to fix your current code:

JSONObject enrollmentResponseObject = 
    (JSONObject) jsonObject.get("enrollment_response");

This gets the inner object. Now you can extract the inner fields:

String condition = (String) enrollmentResponseObject.get("condition");

And so forth. The whole library simply extends a Hashmap (without using generics) and makes you figure out and cast to the appropriate types.

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

Comments

2

Below line,

String name = (String) jsonObject.get("enrollment_response");

should be

String name = jsonObject.getJSONObject("enrollment_response").getString("condition");

Value of enrollment_response is again a Json.

3 Comments

I get an error message as "The method getJSONObject(String) is undefined for the type JSONObject"
Can you provide the fully qualified class name for JSONObject. I assumed you are using org.json library.
@ShashankKadne It's org.json.simple and it's awful.
0

This works, but trust me: change library.

JSONObject jsonObject = (JSONObject) obj;

JSONObject name = (JSONObject) jsonObject.get("enrollment_response");
System.out.println("Condition:" + name);

String name1 = (String) name.get("extra");
System.out.println("extra: " + name1);

5 Comments

Hi enrichman i got an error as "The method getJSONObject(String) is undefined for the type JSONObject"
As the other answerer posted we thought you were using the org.json library. Can you provide the exact library you're using?
@BrianRoach can I suggest you to switch over the standard and more documented org.json or Jackson?
@Enrichman I'm not the OP; I was pointing out what lib they're using ;) See my answer.
@BrianRoach ahaha, sorry, you're right! Never seen such an awful JSON library btw. I had to download it to understand it.

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.