JSON 带有空格的键到 Java POJO
JSON key with blank spaces to a Java POJO
我有以下 JSON
,问题是 Win the match
标签因为空格:
{
"api": {
"results": 10,
"odds": {
"Win the match": {
"1": {
"label": "1",
"pos": "1",
"odd": "1.51"
},
"2": {
"label": "2",
"pos": "3",
"odd": "4.90"
},
"N": {
"label": "N",
"pos": "2",
"odd": "3.05"
}
}
}
}
}
在我的其中一个 Java Class POJO
:
import com.fasterxml.jackson.annotation.JsonProperty;
public class OddsClass {
private The1_N22_EMT winTheMatch;
@JsonProperty("Win the match")
public The1_N22_EMT getWinTheMatch() { return winTheMatch; }
@JsonProperty("Win the match")
public void setWinTheMatch(The1_N22_EMT value) { this.winTheMatch = value; }
}
问题是这个 属性 映射不正确。拜托,我需要你的帮助来进行正确的映射。
编辑
JSON payload 比你想象的要简单。对于动态键,使用 Map
。在你的情况下,你有 Map
in Map
因为你有两级动态键。有时从 JSON
生成 POJO
非常复杂。您应该从 JSON Schema
生成 POJO
模型。您的模型可能如下所示:
class ApiWrapper {
private Api api;
// getters, setters, toString
}
class Api {
private int results;
private Map<String, Map<String, Match>> odds;
// getters, setters, toString
}
class Match {
private String label;
private String pos;
private String odd;
// getters, setters, toString
}
编辑前
你有两个棘手的案例:
- 带空格的键 - 需要使用
JsonProperty
注释。
- 具有动态键的对象 - 它应该映射到
Map<String, ?>
。
POJO
模型的简单示例:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
ApiWrapper api = mapper.readValue(jsonFile, ApiWrapper.class);
System.out.println(api);
}
}
class ApiWrapper {
private Api api;
// getters, setters, toString
}
class Api {
private int results;
private Odds odds;
// getters, setters, toString
}
class Odds {
@JsonProperty("Win the match")
private Map<String, Match> winTheMatch;
// getters, setters, toString
}
class Match {
private String label;
private String pos;
private String odd;
// getters, setters, toString
}
另请参阅:
我有以下 JSON
,问题是 Win the match
标签因为空格:
{
"api": {
"results": 10,
"odds": {
"Win the match": {
"1": {
"label": "1",
"pos": "1",
"odd": "1.51"
},
"2": {
"label": "2",
"pos": "3",
"odd": "4.90"
},
"N": {
"label": "N",
"pos": "2",
"odd": "3.05"
}
}
}
}
}
在我的其中一个 Java Class POJO
:
import com.fasterxml.jackson.annotation.JsonProperty;
public class OddsClass {
private The1_N22_EMT winTheMatch;
@JsonProperty("Win the match")
public The1_N22_EMT getWinTheMatch() { return winTheMatch; }
@JsonProperty("Win the match")
public void setWinTheMatch(The1_N22_EMT value) { this.winTheMatch = value; }
}
问题是这个 属性 映射不正确。拜托,我需要你的帮助来进行正确的映射。
编辑
JSON payload 比你想象的要简单。对于动态键,使用 Map
。在你的情况下,你有 Map
in Map
因为你有两级动态键。有时从 JSON
生成 POJO
非常复杂。您应该从 JSON Schema
生成 POJO
模型。您的模型可能如下所示:
class ApiWrapper {
private Api api;
// getters, setters, toString
}
class Api {
private int results;
private Map<String, Map<String, Match>> odds;
// getters, setters, toString
}
class Match {
private String label;
private String pos;
private String odd;
// getters, setters, toString
}
编辑前
你有两个棘手的案例:
- 带空格的键 - 需要使用
JsonProperty
注释。 - 具有动态键的对象 - 它应该映射到
Map<String, ?>
。
POJO
模型的简单示例:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
ApiWrapper api = mapper.readValue(jsonFile, ApiWrapper.class);
System.out.println(api);
}
}
class ApiWrapper {
private Api api;
// getters, setters, toString
}
class Api {
private int results;
private Odds odds;
// getters, setters, toString
}
class Odds {
@JsonProperty("Win the match")
private Map<String, Match> winTheMatch;
// getters, setters, toString
}
class Match {
private String label;
private String pos;
private String odd;
// getters, setters, toString
}
另请参阅: