这是我的
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\"}}}
关于java - 如何从响应实体获取正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578502/