使用 Jackson 库序列化 JSON 的正确 POJO
Proper POJO for serializing JSON with Jackson library
所以问题是如何为 JSON 正确构建 POJO,如下所示:
{"1":{
"value_1":"something",
"value_2":"something else"},
"2": {
"value_1":"something",
"value_2":"something else"},
"3": {
"value_1":"something",
"value_2":"something else"},
...
"999": {
"value_1":"something",
"value_2":"something else"}}
问题是我无法解析 JSON,如 Google Developers docs 所示,因为保存我需要的数据的数组有很多不同的名称。
我建议将您的 Json 调整为如下内容:
[{“id”: 1, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]},
{“id”: 2, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]},
{“id”: 3, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]},
…
{“id”: 999, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]}]
你可以试试这样解析(剧透很多代码)
public class ParseTo {
private Map<String, InnerParse> unknown = new HashMap<>();
@JsonAnyGetter
public Map<String, InnerParse> get(){
return unknown;
}
@JsonAnySetter
public void set(String key, Object value){
if(value instanceof Map){
Map<String, String> m = (Map<String, String>) value;
unknown.put(key, new InnerParse(m));
}
}
}
public class InnerParse {
private static final String V1_PROPERTY = "value_1";
private static final String V2_PROPERTY = "value_2";
public InnerParse(Map<String, String> map){
this.value1 = map.get(V1_PROPERTY);
this.value2 = map.get(V2_PROPERTY);
}
private String value1;
private String value2;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
}
public class Main {
public static void main(String[] args) {
String jsonString = "{\"1\":{\n" +
" \"value_1\":\"something\",\n" +
" \"value_2\":\"something else\"},\n" +
" \"2\": {\n" +
" \"value_1\":\"something\",\n" +
" \"value_2\":\"something else\"}" +
"}";
try {
ParseTo parsed = new ObjectMapper().readValue(jsonString, ParseTo.class);
parsed.get().entrySet().stream()
.forEach(entry -> {
System.out.println("key: " + entry.getKey() + ", value: (" +
" Inner value1: " + entry.getValue().getValue1() + "," +
" Inner value2: " + entry.getValue().getValue2() +
")");
});
} catch (Exception e){
e.printStackTrace();
}
}
}
依赖关系:
com.fasterxml.jackson.core 杰克逊核心
com.fasterxml.jackson.core 杰克逊注释
com.fasterxml.jackson.core 杰克逊数据绑定
所以问题是如何为 JSON 正确构建 POJO,如下所示:
{"1":{
"value_1":"something",
"value_2":"something else"},
"2": {
"value_1":"something",
"value_2":"something else"},
"3": {
"value_1":"something",
"value_2":"something else"},
...
"999": {
"value_1":"something",
"value_2":"something else"}}
问题是我无法解析 JSON,如 Google Developers docs 所示,因为保存我需要的数据的数组有很多不同的名称。
我建议将您的 Json 调整为如下内容:
[{“id”: 1, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]},
{“id”: 2, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]},
{“id”: 3, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]},
…
{“id”: 999, “values”: [{“value”: 1, “text”: “something”}, {“value”: 2, “text”: “something else”}]}]
你可以试试这样解析(剧透很多代码)
public class ParseTo {
private Map<String, InnerParse> unknown = new HashMap<>();
@JsonAnyGetter
public Map<String, InnerParse> get(){
return unknown;
}
@JsonAnySetter
public void set(String key, Object value){
if(value instanceof Map){
Map<String, String> m = (Map<String, String>) value;
unknown.put(key, new InnerParse(m));
}
}
}
public class InnerParse {
private static final String V1_PROPERTY = "value_1";
private static final String V2_PROPERTY = "value_2";
public InnerParse(Map<String, String> map){
this.value1 = map.get(V1_PROPERTY);
this.value2 = map.get(V2_PROPERTY);
}
private String value1;
private String value2;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
}
public class Main {
public static void main(String[] args) {
String jsonString = "{\"1\":{\n" +
" \"value_1\":\"something\",\n" +
" \"value_2\":\"something else\"},\n" +
" \"2\": {\n" +
" \"value_1\":\"something\",\n" +
" \"value_2\":\"something else\"}" +
"}";
try {
ParseTo parsed = new ObjectMapper().readValue(jsonString, ParseTo.class);
parsed.get().entrySet().stream()
.forEach(entry -> {
System.out.println("key: " + entry.getKey() + ", value: (" +
" Inner value1: " + entry.getValue().getValue1() + "," +
" Inner value2: " + entry.getValue().getValue2() +
")");
});
} catch (Exception e){
e.printStackTrace();
}
}
}
依赖关系:
com.fasterxml.jackson.core 杰克逊核心
com.fasterxml.jackson.core 杰克逊注释
com.fasterxml.jackson.core 杰克逊数据绑定