Json解析并分配给DTO

omvjsjqw  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(150)

将JSON解析为DTO时出现问题。
JSON响应是:

{
  "content": [
    {
      "id": 350,
      "reg": "FA-2001",
      "Email": "abc@gmail.in",
      "Mobile": "+9192000000",
      "Name": "dr kumar",
      "Ip": "0:0:0:0:0:0:0:1",
      "Datetime": "2022-10-20T13:50:49",
    }
  ],
  "pageable": {
    "sort": {
      "unsorted": false,
      "sorted": true,
      "empty": false
    },
    "pageNumber": 0,
    "pageSize": 20,
    "offset": 0,
    "paged": true,
    "unpaged": false
  },
  "last": true,
  "totalPages": 1,
  "totalElements": 1,
  "first": true,
  "sort": {
    "unsorted": false,
    "sorted": true,
    "empty": false
  },
  "numberOfElements": 1,
  "size": 20,
  "number": 0,
  "empty": false
}

DTO

@Data
public class OtherResponse {
    @JsonProperty(value = "content")
    private Map content;
}

获取

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.LinkedHashMap<java.lang.Object,java.lang.Object> from Array value (token JsonToken.START_ARRAY)

请建议如何在Map或其他对象中获取值我的主要关注点是发送特定的键值对作为响应。

mqxuamgl

mqxuamgl1#

在你的json String中,content是一个List。你可以做的是相应地更新OtherResponse

@Data
public class OtherResponse {
    @JsonProperty(value = "content")
    private List<Map<String, String>> content;

    private Pageable pageable;
}

或者甚至为它定义一个专用类:

@Data
public class OtherResponse {
    @JsonProperty(value = "content")
    private List<Person> content;

    private Pageable pageable;
}

PS:更新了答案,增加了Pageable字段。我在这里使用的是从org.springframework.data.domain.Pageable导入的Pageable接口

相关问题