[英]how to get body from the response entity
这是我的
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
您可以使用以下方法实现:
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\"}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.