所以问题是如何为JSON正确构建POJO,如下所示:
char mbuf[16];
strncpy(mbuf, "12345678901234567890", 15);
问题是我无法解析Google Developers docs中所示的JSON,因为有一个数组有许多不同的名称来保存我需要的数据。
答案 0 :(得分:1)
您可以尝试像这样解析(扰乱很多代码)
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 jackson-core
com.fasterxml.jackson.core jackson-annotations
com.fasterxml.jackson.core jackson-databind
答案 1 :(得分:0)
我建议将你的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”}]}]