[英]Map JSON to POJO For Processing - Unrecognized field not marked as ignorable
I need help in mapping below JSON structure for further processing.我需要帮助映射以下 JSON 结构以进行进一步处理。
[{
"userId": "11",
"otherId": "a",
"key1": "Tesla",
"key2": "S3",
"behaviour": {
"color": "white",
"size": "S",
"owner": "Mr. A"
}
},
{
"userId": "22",
"otherId": "",
"key1": "Merc",
"key2": "C-Class",
"behaviour": {
"color": "black",
"size": "M",
"isNew": true
}
},
{
"userId": "33",
"otherId": "c",
"key1": "Honda",
"key2": "CRV",
"behaviour": {
"color": "green",
"size": "L",
}
}]
Below is the POJO i have:以下是我拥有的 POJO:
public class MainObject {
private String userId;
private String otherId;
private String key1;
private String key2;
private Set<Behaviour> behaviours;
}
public class Behaviour {
private final String name;
private final Object value;
}
I need to get the list of MainObject for further processing.我需要获取 MainObject 列表以进行进一步处理。 Tried below but not sure how can i map for behaviour set -在下面尝试过,但不确定如何映射行为集 -
String inputLine = currentBufferedReader.readLine();
//Above String has complete JSON
ObjectMapper objectMapper = new ObjectMapper();
FirstObject[] firstObjects = objectMapper.readValue(inputLine, FirstObject[].class);
I am getting: Unrecognized field not marked as ignorable with above code.我得到:无法识别的字段未使用上述代码标记为可忽略。 Please suggest.请建议。
Define ur class like belwo像belwo一样定义你的类
class MainObject {
public String userId;
public String otherId;
public String key1;
public String key2;
@JsonProperty(value = "behaviour")
public Map<String, String> behaviours;
@Override
public String toString() {
return "MainObject [userId=" + userId + ", otherId=" + otherId + ", key1=" + key1 + ", key2=" + key2
+ ", behaviours=" + behaviours + "]";
}
}
Code to map要映射的代码
public static void main(String[] args) throws IOException {
String array = "[\r\n" +
" {\r\n" +
" \"userId\":\"11\",\r\n" +
" \"otherId\":\"a\",\r\n" +
" \"key1\":\"Tesla\",\r\n" +
" \"key2\":\"S3\",\r\n" +
" \"behaviour\":{\r\n" +
" \"color\":\"white\",\r\n" +
" \"size\":\"S\",\r\n" +
" \"owner\":\"Mr. A\"\r\n" +
" }\r\n" +
" },\r\n" +
" {\r\n" +
" \"userId\":\"22\",\r\n" +
" \"otherId\":\"\",\r\n" +
" \"key1\":\"Merc\",\r\n" +
" \"key2\":\"C-Class\",\r\n" +
" \"behaviour\":{\r\n" +
" \"color\":\"black\",\r\n" +
" \"size\":\"M\",\r\n" +
" \"isNew\":true\r\n" +
" }\r\n" +
" },\r\n" +
" {\r\n" +
" \"userId\":\"33\",\r\n" +
" \"otherId\":\"c\",\r\n" +
" \"key1\":\"Honda\",\r\n" +
" \"key2\":\"CRV\",\r\n" +
" \"behaviour\":{\r\n" +
" \"color\":\"green\",\r\n" +
" \"size\":\"L\"\r\n" +
" }\r\n" +
" }\r\n" +
"]";
ObjectMapper mapper = new ObjectMapper();
MainObject[] objects = mapper.readValue(array, MainObject[].class);
System.out.println(Arrays.asList(objects));
}
output输出
[MainObject [userId=11, otherId=a, key1=Tesla, key2=S3, behaviours={color=white, size=S, owner=Mr. A}],
MainObject [userId=22, otherId=, key1=Merc, key2=C-Class, behaviours={color=black, size=M, isNew=true}],
MainObject [userId=33, otherId=c, key1=Honda, key2=CRV, behaviours={color=green, size=L}]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.