[英]Parse JSON to Java object with dynamics objects in JSON-response
I am trying to parse a JSON-response to a Java object and then I want to save it to a Postgresql.我正在尝试解析对 Java object 的 JSON 响应,然后我想将其保存到 Postgresql。
I have the following json-response:我有以下 json 响应:
{
"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:我开始像这样创建 class:
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在结果 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?正确的方法是如何做到这一点,或者我必须为每个卡和角 object 创建一个 class? This is just an example but in my real case I have like 20 of this objects.
这只是一个例子,但在我的真实案例中,我有 20 个这样的对象。 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.
我还想将 object 键名设置为变量,因为我想将 map 与数据库中的字段相对应。
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.
我建议直接使用 JSON 查询库转换 JSON。 For example, use Josson .
例如,使用乔森。
https://github.com/octomix/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 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"
} ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.