如何从json反序列化为java对象?

How deserialize from json to java object?

提问人:Khaski 提问时间:5/22/2023 最后编辑:Khaski 更新时间:5/22/2023 访问量:101

问:

我的问题是我需要将来自服务器的 json 响应反序列化为 java 对象,但问题是反序列化后,set 字段中只有 null。来自服务器的答案检查了填写的字段来了。我做错了什么以及如何解决?感谢您的帮助。

This my method 服务

 public ReportDataFromReporter getReports(int weekNumber, int year) throws JsonProcessingException {
    LocalDate mondayOfWeek = LocalDate.of(year, Month.JUNE, 1)
            .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
            .with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber);
    LocalDate sundayOfWeek = mondayOfWeek.plusDays(6);
    String url = "example.com";
    ObjectMapper objectMapper = new ObjectMapper()
            .registerModule(new JavaTimeModule());
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    objectMapper.setDateFormat(dateFormat);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ObjectForRequestToReporter objectForRequestToReporter = new ObjectForRequestToReporter();
    objectForRequestToReporter.setToken(userService.getUser().getReporterToken());
    objectForRequestToReporter.setEmployee(userService.getUser().getLogin());
    objectForRequestToReporter.setFrom(mondayOfWeek);
    objectForRequestToReporter.setTo(sundayOfWeek);
    String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);
    System.out.println(jsonObject);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(jsonObject, httpHeaders);
    String reporterResponse = restTemplate.postForObject(url, entity, String.class);
    ReportDataFromReporter reportDataFromReporter = objectMapper
            .readValue(reporterResponse, ReportDataFromReporter.class);
    System.out.println(reportDataFromReporter);
    return reportDataFromReporter;

这是我需要反序列化的 Java 对象

 @Data
 public class ReportDataFromReporter {
private Long id;
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate day;
private Integer hours;
private String description;
private Long project;
private Long taskType;
private Long work_after;
 }

来自 json 中外部 API 的此响应

 {
   "reports": [
     {
      "id": somenumber,
  "date": "2023-03-15T00:00:00+03:00",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
},
{
  "id": somenumber,
  "date": "2023-03-15T00:00:00+03:00",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
},
{
   "id": somenumber,
  "date": "somedate",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
},
{
"id": somenumber,
  "date": "2023-03-15T00:00:00+03:00",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
     }
   ]
 }
弹簧

评论


答:

1赞 Michael Gantman 5/22/2023 #1

类的任何属性都与 Json 中的文件名不匹配。此外,Json 中的字段应映射到 type 或 ZonedDateTime。要正确映射日期字段,请参阅以下问题的答案以获取更多详细信息:Spring Data JPA - 用于 json 序列化的 ZonedDateTime 格式。至于与字段名称不匹配的属性,您应该使用@JsonProperty注释。例如,代替dateOffsetDateTime

private Long project;

@JsonProperty("pro")
private Long project;

这将告诉 ObjectMapper 将字段“pro”映射到属性“project”。请参阅此文章:Jackson 注释示例

评论

0赞 Khaski 5/22/2023
我不仅对日期有问题,在期望化每个字段都为 null 之后
0赞 Michael Gantman 5/22/2023
我更新了我的答案以解决您的问题
0赞 Khaski 5/23/2023
我为 java 对象中的每个字段添加了注释@JsonProperty,但仍然有 null
1赞 Michael Gantman 5/23/2023
Json 字符串是一个列表,而不是单个值。因此,尝试更改行: ReportDataFromReporter reportDataFromReporter = objectMapper .readValue(reporterResponse, ReportDataFromReporter.class);到 List<ReportDataFromReporter> = objectMapper .readValue(reporterResponse, List.class);但我怀疑你会得到 List<Map>而不是 List<ReportDataFromReporter>。因此,在这种情况下,您需要遍历列表中的每个地图并将其转换为 ReportDataFromReporter
0赞 Khaski 5/24/2023
你能再帮忙一次如何将其转换为ReportDataFromReporter吗?