how to get body from the response entity

Viewed 22484

This is my

ResponseEntity<String> response= new ResponseEntity<String> (
"\"<200 OK OK,{\\\"status\\\":200,\\\"success\\\":true,\\\"info\\\":{\\\"mid\\\":{\\\"id\\\":\\\"95706\\\"}}},[]>\"", HttpStatus.OK);

how to extract json from this response?

tried response.getBody() but giving me entire string.

Any help would be appreciated

response.getBody() but giving me entire string.
ResponseEntity<String> response= new ResponseEntity<String> (
"\"<200 OK OK,{\\\"status\\\":200,\\\"success\\\":true,\\\"info\\\":{\\\"mid\\\":{\\\"id\\\":\\\"95706\\\"}}},[]>\"", HttpStatus.OK);

response.getBody(); giving entire string not the json

1 Answers

You can achieve using:

ResponseEntity<String> response= new ResponseEntity<String> ("\"<200 OK OK,{\\\"status\\\":200,\\\"success\\\":true,\\\"info\\\":{\\\"mid\\\":\\\"id\\\":\\\"95706\\\"}}},[]>\"", HttpStatus.OK);


String responseStr = response.getBody();
int begin = responseStr.indexOf("{");
int end = responseStr.lastIndexOf("}") + 1;

responseStr = responseStr.substring(begin, end);
System.out.println(responseStr);

It will print:

{\"status\":200,\"success\":true,\"info\":{\"mid\":{\"id\":\"95706\"}}}
Related