java - 将 JSON 解析为 JAVA 时出现问题
问题描述
我正在尝试使用 Jackson ObjectMapper 将 JSON-String 解析为 Java。
我有一根短弦让我头疼。它看起来很精简:
{
"api": {
"results": 16,
"statistics": {
"Fouls": {
"home": "10",
"away": "7"
}
}
}
}
我尝试了不同的注释并在 google 和 stackoverflow 上进行了搜索,但找不到解决方案。我得到的错误如下:线程“main”org.codehaus.jackson.map.exc.UnrecognizedPropertyException中的异常:无法识别的字段“Fouls”
如果我将犯规更改为犯规,它可以正常工作,但问题是我不拥有该文件并且无法更改它
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
public class StatisticsDAO {
public static class StatisticsApi {
private Statistics statistics;
public StatisticsApi() {
super();
}
public Statistics getStatistics() {
return statistics;
}
public void setApi(Statistics statistics) {
this.statistics = statistics;
}
}
public static class Statistics {
private Long results;
private Statistic statistics;
private Fouls Fouls;
public Statistics() {
super();
}
public Long getResults() {
return results;
}
public void setResults(Long results) {
this.results = results;
}
public Statistic getStatistics() {
return statistics;
}
public void setStatistics(Statistic statistics) {
this.statistics = statistics;
}
}
public static class Statistic {
private Fouls Fouls;
public Statistic() {
}
public Fouls getFouls() {
return Fouls;
}
public void setFouls(Fouls Fouls) {
this.Fouls = Fouls;
}
}
public static class Fouls {
private Long home;
private Long away;
public Long getHome() {
return home;
}
public void setHome(Long home) {
this.home = home;
}
public Long getAway() {
return away;
}
public void setAway(Long away) {
this.away = away;
}
}
}
JSON-String 更长,但我在这里将其最小化。我对这个字符串还有另一个问题。例如,如果犯规是如下所示的“犯规主队”,我该如何处理。我如何处理空间?
{
"api": {
"results": 16,
"statistics": {
"Fouls Hometeam": {
"home": "10",
"away": "7"
}
}
}
}
我的 ObjectMapper 如下所示:
import org.codehaus.jackson.map.ObjectMapper;
public class StatisticsBO {
public static void main(String args[]) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String statisticsFile = RestClient.restClient(URL);
StatisticsApi statisticsApi = mapper.readValue(statisticsFile, StatisticsDAO.StatisticsApi.class);
}
- 如何解决犯规上大写字母的问题?
- 当像“Fouls Hometeam”这样的字段名称中有空格时,我该如何解决我的问题?
我希望我有所有必要的信息。如果没有,请问我,我会尽力提供信息。
解决方案
我早些时候尝试过 JsonProperty 但没有让它工作。但是多亏了@NoDataFound,我让它工作了。
推荐阅读
- javascript - Mutations-Observer 获得添加元素
- r - 将嵌套列表的第二个元素设置为列表元素的名称 (purrr)
- postgresql - 如何从 Postgres 转储中 pg_restore 一个表及其模式?
- node.js - 如何使用 xPath 在 Puppeteer 中单击元素
- sql - SQL 返回相同的数据或重复
- apache-kafka - Kafka 流在从处理器转发消息时抛出 RecordTooLargeException 异常
- javascript - React Hooks - 未捕获的不变违规:对象作为 React 子对象无效
- r - 如何使用 R 上的传单在 Lambert 93 中投影光栅+多边形?
- ms-word - 如何在后台视图中将焦点设置在自定义选项卡上
- uikit - 从 UIImage 转换为 SwiftUI Image 会导致相同大小的空白图像