我有一个通用控制器,它将返回json值,它将从任何API获得。 我试图通过放入一个对象来发送响应,但是它附加/在每个",请建议如何将结果作为json值发送。
Response oResponse=new Response();
Map<String,Object> result=(Map<String,Object>) service.getData();
res.setData(result.get("result"));
res.setResponseCode("200");
res.setResponseMessage("API result fetched successfully");
RESULT :
{
"responseCode": "000",
"responseMessage": "API result fetched successfully",
"data": "{\"info\":\"SUCCESS\",\"warn\":\"\",\"error\":\"\",\"responseCode\":\"200\",\"responseData\":{\"logindetails\":{\"host\":\"localhost:8080\",\"connection\":\"keep-alive\",\"content-length\":\"145\",\"origin\":\"chrome-extension:\\/\\/fhbjgbiflinjbdggehcddcbncdddomop\",\"apiurl\":\"abc-web-api\\/12345wer\\/testE2B\\/login\",\"user-agent\":\"Mozilla\\/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/58.0.3029.110 Safari\\/537.36\",\"content-type\":\"multipart\\/form-data; boundary=----WebKitFormBoundary6NUlvB1E7FBrRORE\",\"projectkey\":\"abc120\",\"cache-control\":\"no-cache\",\"postman-token\":\"81df53c2-7702-2691-f168-b544daa0e20b\",\"token\":\"YJJ7Y2khYj3he+4aNJ9WYCFLJfhGrpNrZ4JMyoiYtjnmh19UYe1vwoPYQ2Q==\",\"accept\":\"*\\/*\",\"accept-encoding\":\"gzip, deflate, br\",\"accept-language\":\"en-GB,en-US;q=0.8,en;q=0.6\"}}}"
}
答案 0 :(得分:0)
您可以在下面生成json响应,您需要 org.json jar文件
您可以发送回复
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(yourData);
resp.getWriter().write(jsonString);
如果您使用的是maven,请将其添加到您的pom.xml
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
答案 1 :(得分:0)
尝试将@JsonRawValue注释添加到数据字段中。 这将按顺序序列化/反序列化json。