-1

I was having some problem when trying to extract information out from JSON. Here is the example of my JSON:

// removed

My responseMessage is just a string. And my code to extract the info out:

// removed 

I tried to follow the solution from this thread However, nothing has been printed out and there is no error message. Any ideas on how to resolve this? Thanks!

EDIT

I realized the problem is due to this error: java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.JSONObject

However, if I changed my import for JSONObject to import org.json.simple.JSONObject; the code above will break. Any ideas?

6
  • What exactly is the error? Also, your json doesn't seem to be valid, you need to put response in curly braces. Commented Dec 19, 2018 at 2:06
  • Down voter mind to explain? Commented Dec 19, 2018 at 2:14
  • Your root doesn't have schedules array but it's nested under response. Apart from this there doesn't seem to be any obvious error. Separate note, paste json instead of image. Commented Dec 19, 2018 at 2:29
  • I'm able to print username with similar code. Though I used gson. Debugging is the only solution. Commented Dec 19, 2018 at 2:46
  • Let us continue this discussion in chat. Commented Dec 19, 2018 at 3:39

3 Answers 3

1

As asked in comments, below code is working using gson

JsonParser parser = new JsonParser();
JsonObject root =  (JsonObject) parser.parse(json);
JsonArray schedulesArray = root.getAsJsonObject("response").getAsJsonArray("schedules");
JsonObject firstSchedule = (JsonObject) schedulesArray.get(0);
String userName = firstSchedule.get("username").getAsString();
Sign up to request clarification or add additional context in comments.

Comments

-1

I have a “stupid” method.First,you should create three DTOs,it's may like this:

public class Schedule {
    private String username;
    private String scheduleId;
    private String SCHEDULEsTARTtIME;
    private String NOTES;
    private String familyName;
    private String principalId;
    private String friendlyName;
    private String scheduleEndTime;

    // get set methods
}

public class ResponseInfo {
    private String instrumentName;
    private String instrumentId;
    private String instrumentType;
    private List<Schedule> schedules;
}

public class Response {
    private ResponseInfo response;
}

then,convert response string to Response DTO

Response newResponse = mapper.readValue(content,Response.class);

2 Comments

Why not just use the JSONParser to parse the string responseMessage to JSON instead?
Just used to it,you must first get the value of key "response" like JSONObject tmp = (JSONObject) new JSONParser().parse(content);JSONObject resp = (JSONObject)tmp.get("response");
-1
JSONArray schedulesArray = root.getJSONArray("schedules");

replace it with the following:

JSONObject response = root.getJSONObject("response");
JSONArray schedulesArray = response.getJSONArray("schedules");

then try again.

1 Comment

Nope it does not work. It is the same as the code snippet I just updated

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.