首页 > 解决方案 > 将 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);

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

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

标签: javajsonjackson

解决方案


我早些时候尝试过 JsonProperty 但没有让它工作。但是多亏了@NoDataFound,我让它工作了。


推荐阅读