Java 如何从响应实体获取主体
这是我的Java 如何从响应实体获取主体,java,spring,spring-boot,spring-mvc,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); response.getBody();提供整个字符串而不是j
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);
你真的需要
String
作为主体,那么为什么它要返回其他东西呢。任何json编码都会帮助你将其转换为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\"}}}