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": {
"corners": {
"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"
}
]
}
}
}
}
]
}
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 |
It is complicated to handle dynamic contents by defining many Classes. I suggest to transform the JSON directly using JSON query library. For example, use Josson .
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{" +
" \"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\"" +
" }" +
" ]" +
" }" +
" }" +
" }" +
" }" +
" ]" +
"}");
Transformation
JsonNode node = josson.getNode(
"results.map(" +
" FI," +
" event_id," +
" categories: entries()" +
" .map(category: key," +
" value.updated_at," +
" value.key," +
" market_id: value.sp.*.id," +
" market: value.sp.*.name," +
" value.sp.*.odds)" +
" .unwind(odds)" +
").unwind(categories)");
System.out.println(node.toPrettyString());
Output
[ {
"FI" : "120986750",
"event_id" : "5164306",
"category" : "cards",
"updated_at" : "1660559432",
"key" : "AAA100",
"market_id" : "1",
"market" : "Cards",
"id" : "101",
"odds" : "2.200",
"header" : "Over",
"name" : "11"
}, {
"FI" : "120986750",
"event_id" : "5164306",
"category" : "cards",
"updated_at" : "1660559432",
"key" : "AAA100",
"market_id" : "1",
"market" : "Cards",
"id" : "102",
"odds" : "8.500",
"header" : "Exactly",
"name" : "11"
}, {
"FI" : "120986750",
"event_id" : "5164306",
"category" : "cards",
"updated_at" : "1660559432",
"key" : "AAA100",
"market_id" : "1",
"market" : "Cards",
"id" : "103",
"odds" : "1.909",
"header" : "Under",
"name" : "11"
}, {
"FI" : "120986750",
"event_id" : "5164306",
"category" : "corners",
"updated_at" : "1660559431",
"key" : "AAA200",
"market_id" : "2",
"market" : "Corners",
"id" : "201",
"odds" : "2.200",
"header" : "Over",
"name" : "10"
}, {
"FI" : "120986750",
"event_id" : "5164306",
"category" : "corners",
"updated_at" : "1660559431",
"key" : "AAA200",
"market_id" : "2",
"market" : "Corners",
"id" : "202",
"odds" : "8.500",
"header" : "Exactly",
"name" : "10"
}, {
"FI" : "120986750",
"event_id" : "5164306",
"category" : "corners",
"updated_at" : "1660559431",
"key" : "AAA200",
"market_id" : "2",
"market" : "Corners",
"id" : "203",
"odds" : "1.909",
"header" : "Under",
"name" : "10"
} ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.