英文:
How deserialize from json to java object?
问题
以下是您要翻译的内容:
My problem is that I need to deserialize my json response from the server into a java object, but the problem is that after deserialization, there are only null in the set fields. The answer from the server checked the filled fields come. What am I doing wrong and how to fix it? Thanks for the help.
This my method service
这是我的方法服务
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 reporterResponse = restTemplate.postForObject(url, entity, String.class);
ReportDataFromReporter reportDataFromReporter = objectMapper
.readValue(reporterResponse, ReportDataFromReporter.class);
System.out.println(reportDataFromReporter);
return reportDataFromReporter;
}
This is the Java object I need to deserialize into
这是我需要反序列化的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;
}
This response from external api in json
这是来自外部API的JSON响应
{
"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,
}
]
}
英文:
My problem is that I need to deserialize my json response from the server into a java object, but the problem is that after deserialization, there are only null in the set fields. The answer from the server checked the filled fields come. What am I doing wrong and how to fix it? Thanks for the help.
This my method service
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;
this is the Java object I need to deserialize into
@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;
}
This response from external api in json
{
"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
得分: 1
以下是您要翻译的内容:
没有您类的属性与Json中的文件名称匹配。此外,Json中的date
字段应映射为OffsetDateTime
类型或ZonedDateTime
。要正确映射日期字段,请查看此问题的答案以获取更多详细信息:Spring Data JPA - ZonedDateTime格式的json序列化。至于不匹配的属性与字段名称,您应该使用@JsonProperty
注解。例如,不要使用:
private Long project;
而是使用:
@JsonProperty("pro")
private Long project;
这将告诉ObjectMapper将字段"pro"映射到属性"project"。请参阅此文章:Jackson注解示例。
英文:
None of your properties of your class match the name of your files in Json. Also the date
field in Json should be mapped to OffsetDateTime
type or ZonedDateTime. To properly map your date field see the answer to this question for more details: Spring Data JPA - ZonedDateTime format for json serialization. As for the unmatching properties to field names you should use @JsonProperty annotation. For instance instead of
private Long project;
use
@JsonProperty("pro")
private Long project;
This will tell ObjectMapper to map field "pro" to property "project". See this article: Jackson Annotation Examples
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论