0

I am trying to parse JSON-String to Java using Jackson ObjectMapper.

I have a short string that gives me headache. It looks lite following:

{
    "api": {
        "results": 16,
        "statistics": {
        "Fouls": {
                "home": "10",
                "away": "7"
            }
        }
    }
}

I have tried different annotations and searched on google and stackoverflow but can´t find a solution. The error I get is the following: Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "Fouls"

If I change the Fouls to fouls it works fine but the problem is that I do not own the file and can´t get it changed

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;
            }   
        }
    }

The JSON-String is longer but I have minimized it here. I also have another problem with this string. How do I handle if Fouls for example had been "Fouls Hometeam" like below. How do I handle the space?

{
    "api": {
        "results": 16,
        "statistics": {
        "Fouls Hometeam": {
                "home": "10",
                "away": "7"
            }
        }
    }
}

My ObjectMapper looks like the following:

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. How do I solve the problem with capital letter on Fouls?
  2. How do I solve my problem when it is a space in the field name like "Fouls Hometeam"?

I hope I have all the necessery information. If not please ask me and I will try to hand the information.

JavaNisse
  • 17
  • 5

1 Answers1

0

I tried JsonProperty earlier but didn´t get it to work. But thanks to @NoDataFound I got it to work.

JavaNisse
  • 17
  • 5