繁体   English   中英

将 JSON 解析为 JAVA 时出现问题

[英]Issue when parsing JSON to 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”

如果我将 Fouls 更改为 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 更长,但我在这里将其最小化。 这个字符串还有另一个问题。 例如,如果 Fouls 是如下所示的“Fouls Hometeam”,我该如何处理。 我如何处理空间?

{
    "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);

}
  1. 如何解决 Fouls 上大写字母的问题?
  2. 当字段名称中有一个空格时,如“Fouls Hometeam”,我该如何解决我的问题?

我希望我有所有必要的信息。 如果没有,请问我,我会尽力提供信息。

我之前尝试过 JsonProperty,但没有成功。 但是多亏了@NoDataFound,我才开始工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM