I am trying to parse a JSON-response to a Java object and then I want to save it to a Postgresql.
I have the following json-response:
{
"success": 1,
"results": [
{
"FI": "120986750",
"event_id": "5164306",
"cards": {
"updated_at": "1660559432",
"key": "AAA100",
"sp": {
"cards": {
"id": "1",
"name": "Cards",
"odds": [
{
"id": "101",
"odds": "2.200",
"header": "Over",
"name": "11"
},
{
"id": "102",
"odds": "8.500",
"header": "Exactly",
"name": "11"
},
{
"id": "103",
"odds": "1.909",
"header": "Under",
"name": "11"
}
]
}
}
},
"corners": {
"updated_at": "1660559431",
"key": "AAA200",
"sp": {
"corners": {
"id": "2",
"name": "Corners",
"odds": [
{
"id": "201",
"odds": "2.200",
"header": "Over",
"name": "10"
},
{
"id": "202",
"odds": "8.500",
"header": "Exactly",
"name": "10"
},
{
"id": "203",
"odds": "1.909",
"header": "Under",
"name": "10"
}
]
},
"total_corners": {
"id": "3",
"name": "Total Corners",
"odds": [
{
"id": "204",
"odds": "17.000",
"name": "Under 6"
},
{
"id": "205",
"odds": "4.333",
"name": "6 - 8"
},
{
"id": "206",
"odds": "2.750",
"name": "9 - 11"
},
{
"id": "207",
"odds": "3.400",
"name": "12 - 14"
},
{
"id": "208",
"odds": "5.500",
"name": "Over 14"
}
]
}
}
}
}
]
}
I started to create a class like this:
public class PreMatchOdds {
@JsonProperty("success")
private Integer success;
@JsonProperty("results")
private List<Result> results = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
private final static long serialVersionUID = -8631993296159893856L;
getter and setters..
Then instead of
@JsonProperty("cards")
private Cards cards;
and
@JsonProperty("corners")
private Corners corners;
I tried with:
@JsonProperty("categories")
@JsonAlias({"cards", "corners"})
private Categories categories;
in the Result class
public class Result implements Serializable {
@JsonProperty("FI")
private String fi;
@JsonProperty("event_id")
private String eventId;
@JsonProperty("categories")
@JsonAlias({"cards", "corners"})
private Categories categories;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
private final static long serialVersionUID = -683759794150688410L;
How is the proper way to do this or do I have to create a class for each cards and corners object? This is just an example but in my real case I have like 20 of this objects. I would also like to set the object key name to a variable since I want to map it against a field in the database.
I have tried and googled but not found what I am looking for or not really understood. In the end I would like to save it to a table in a database like this:
| FI | event_id | category | updated_at | key | market_id | market | odds_id | odds | header | handicap | name |
|---|---|---|---|---|---|---|---|---|---|---|---|
| FI value | event_id | value | "cards" | updated_at | AAA100 | cards.id | cards.name | odds.id | odds.odds | odds.header | odds.handicap |
| "corners" | AAA200 | corners.id | corners.name | odds.id | odds.odds | odds.header | odds.handicap | odds.name | |||
| Example | |||||||||||
| 120986750 | 5164306 | cards | 1660559432 | AAA100 | 1 | Cards | 101 | 2.2 | Over | 11 | |
| 120986750 | 5164306 | cards | 1660559432 | AAA100 | 1 | Cards | 102 | 8.5 | Exatly | 11 | |
| 120986750 | 5164306 | cards | 1660559432 | AAA100 | 1 | Cards | 103 | 1.909 | Under | 11 | |
| 120986750 | 5164306 | corners | 1660559431 | AAA200 | 2 | Corners | 201 | 2.2 | Over | 10 | |
| 120986750 | 5164306 | corners | 1660559431 | AAA200 | 2 | Corners | 202 | 8.5 | Exatly | 10 | |
| 120986750 | 5164306 | corners | 1660559431 | AAA200 | 2 | Corners | 203 | 1.909 | Under | 10 |