json - Spring Boot以可分页的JSON格式显示转义的JSON字符串
问题描述
我有一个包含 JSON 字符串的字符串类型的列。检索数据时,值列显示为转义字符串,如下所示:
"content": [
{
"value": "{\n \"city\": {\n \"coord\": {\n \"lat\": 33.34058,\n \"lon\": 44.400879\n },\n \"country\": \"IQ\",\n \"findname\": \"BAGHDAD\",\n \"id\": 98182,\n \"name\": \"Baghdad\",\n \"zoom\": 1\n },\n \"clouds\": {\n \"all\": 0\n },\n \"main\": {\n \"humidity\": 48,\n \"pressure\": 1018,\n \"temp\": 286.53,\n \"temp_max\": 289.15,\n \"temp_min\": 285.15\n },\n \"time\": 1555995695,\n \"weather\": [\n {\n \"description\": \"smoke\",\n \"icon\": \"50d\",\n \"id\": 711,\n \"main\": \"Smoke\"\n }\n ],\n \"wind\": {\n \"deg\": 260,\n \"speed\": 4.1\n }\n}",
"time": "2019-04-22T22:01:35+12:00"
},
{
"value": "{\n \"city\": {\n \"coord\": {\n \"lat\": 33.34058,\n \"lon\": 44.400879\n },\n \"country\": \"IQ\",\n \"findname\": \"BAGHDAD\",\n \"id\": 98182,\n \"name\": \"Baghdad\",\n \"zoom\": 1\n },\n \"clouds\": {\n \"all\": 0\n },\n \"main\": {\n \"humidity\": 48,\n \"pressure\": 1018,\n \"temp\": 286.53,\n \"temp_max\": 289.15,\n \"temp_min\": 285.15\n },\n \"time\": 1555995695,\n \"weather\": [\n {\n \"description\": \"smoke\",\n \"icon\": \"50d\",\n \"id\": 711,\n \"main\": \"Smoke\"\n }\n ],\n \"wind\": {\n \"deg\": 260,\n \"speed\": 4.1\n }\n}",
"time": "2019-04-22T22:01:35+12:00"
}
]
我只是想知道如何以正确的 JSON 格式返回值,如下所示。我怎么能轻易做到呢?
{
"value": {
"city": {
"coord": {
"lat": 28.383329,
"lon": 36.583328
},
"country": "SA",
"findname": "TABUK",
"id": 101628,
"name": "Tabuk",
"zoom": 5
},
"clouds": {
"all": 20
},
"main": {
"humidity": 30,
"pressure": 1013,
"temp": 291.15,
"temp_max": 291.15,
"temp_min": 291.15
},
"time": 1556420489,
"weather": [
{
"description": "few clouds",
"icon": "02n",
"id": 801,
"main": "Clouds"
}
],
"wind": {
"deg": 270,
"speed": 3.6
}
},
"time": "2019-04-22T22:01:35+12:00"
}
这是我的代码:
型号/实体:
@Entity
@Table(name = "WEATHER_14_TOTAL")
public class Some {
@Column(name = "V")
private String value;
@Id
@Column(name = "T")
private ZonedDateTime time;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public ZonedDateTime getTime() {
return time;
}
public void setTime(ZonedDateTime time) {
this.time = time;
}
}
存储库:
@RepositoryRestResource(collectionResourceRel = "some", path = "some")
public interface SomeRepository extends JpaRepository<Some, ZonedDateTime> {
}
控制器:
@RepositoryRestController
@RequestMapping("/somes")
public class SomeController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
public SomeService someService;
@Autowired
public SomeRepository someRepository;
@GetMapping
public @ResponseBody ResponseEntity<Page<Some>> getAllPaged(
@RequestParam(defaultValue = "0") Integer pageNo,
@RequestParam(defaultValue = "2") Integer pageSize,
@RequestParam(defaultValue = "time") String sortBy) {
Pageable paging = PageRequest.of(pageNo, pageSize, Sort.by(sortBy));
Page<Some> pagedResult = someRepository.findAll(paging);
return ResponseEntity.ok().body(pagedResult);
}
}
解决方案
推荐阅读
- java - 在 Java 流编程中,为什么我无法将所有对象的某个字段设置为 null?
- java - 我们如何在 Spring Boot Filter 中获取和设置响应体
- javascript - 反应:数组包含元素,但 array.length 输出到控制台为零
- python-3.x - SqlAlchemy 中表内的多对一关系
- sql - 如何在聚合函数中找到前 5 个值
- c++ - 初始化 constexpr(字符串?)时使用 #include
- ios - 如何让托管的nodejs网站在cordova上运行?
- python - pyenv 在 MACOS 上安装 python 2.7.8 失败
- laravel-5 - Laravel 5.8 依赖注入 - 如何将模型注入服务
- php - codeigniter 文件上传,两次提交到数据库