首页 > 解决方案 > 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);
    }
}

标签: jsonspringjpaescapingpageable

解决方案


推荐阅读