-1

I hit the API, I get the response as

{"headers":{"Keep-Alive":["timeout=4, max=500"],"Server":["Apache"],"X-Content-Type-Options":["nosniff"],"Connection":["Keep-Alive"],"Vary":["X-Forwarded-For"],"X-XSS-Protection":["1;mode=block"],"Content-Length":["451"],"Content-Type":["application/hal+json"]},"statusCodeValue":200,"body":"{\"id\":\"7199\",\"username\":\"[email protected]\",\"start_time\":1583212261,\"expire_time\":1583253338,\"sponsor_profile\":\"1\",\"enabled\":false,\"current_state\":\"disabled\",\"notes\":null,\"visitor_carrier\":null,\"role_name\":\"[Guest]\",\"role_id\":2,}

Then I try to fetch the body.I get till body but I m not able to fetch username under body.Basically my main aim is to get the username.It throws this error

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

Logic that I tried to get username.

ResponseEntity<String> resp = restTemplate.exchange(
                            reader.getAccountURL() + request.getUsername(),
                            HttpMethod.GET, entity, String.class);
JSONObject accountDetails = new JSONObject(resp);
Object getBody =  accountDetails.get("body");
Object alreadyExits = ((JSONObject) getBody).get("username");

What m I doing wrong?

4
  • Try getJsonObject("body") instead of get("body") . Commented Mar 4, 2020 at 10:01
  • 1
    It looks like the response you have is a JSON-ification of a HTTP response. Therefore the body is simply a string value taht encodes another JSON object, so you'll need to parse the body value again. Commented Mar 4, 2020 at 10:01
  • @Arnaud When I do that I get this exception org.json.JSONException: JSONObject["body"] is not a JSONObject. Commented Mar 4, 2020 at 10:05
  • @JoachimSauer.Can you show me how? Commented Mar 4, 2020 at 10:08

2 Answers 2

2

follow the steps:

  1. get body string: String bodyString= resp.getString("body");
  2. parse bodyString to jsonObject: JSONObject body= new JSONObject(bodyString);
  3. get the username: String usename= body.getString("username");

This should work.

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

6 Comments

I am assuming, your body is string of valid json object
I tried it but it thows me an error saying username not found
print what you get in your body string, see if it contains username and better share your code and output
It does contain username.I have shown it in my post.
this shouldn't happen. Did you try the way I said above? if yes,can you share your code and error stacktrace please?
|
0

JSONObject is nothing but a map which works on key-value. If the value returned by a key is map(i.e. key-value pairs) then it can be cast into a JSONObject, but in your case getBody.get("username") returns [email protected] which is a simple string and not a key-value pair hence you get this exception Use: JSONObject getBody = accountDetails.getJsonObject("body") or you can use:

String bodyString= accountDetails.getString("body");
JSONObject getBody= new JSONObject(bodyString)

and then use Object alreadyExits = ((String) getBody).get("username"); and it should work just fine.

4 Comments

when I try it gives me this:The method get(String) is undefined for the type String
you have to make body also into jsonobject for which i have updated my answer
I tried it but I get the same error which I mentioned in my post
The json mentioned in the post is not a valid json, so can you please give the correct json so that it can be debugged..you can validate it on jsonformatter.curiousconcept.com

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.