如何从响应实体获取身体

时间:2019-06-13 10:28:06

标签: java spring spring-boot spring-mvc

这是我的

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

如何从此响应中提取json?

尝试了response.getBody(),但给了我整个字符串。

任何帮助将不胜感激

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();给出整个字符串而不是json

1 个答案:

答案 0 :(得分:0)

您可以使用:

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);

它将打印:

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