英文:
Map JSON to POJO For Processing - Unrecognized field not marked as ignorable
问题
我需要帮助将下面的 JSON 结构映射到后续处理中。
[{
"userId": "11",
"otherId": "a",
"key1": "特斯拉",
"key2": "S3",
"behaviour": {
"color": "白色",
"size": "S",
"owner": "A 先生"
}
},
{
"userId": "22",
"otherId": "",
"key1": "梅赛德斯",
"key2": "C-Class",
"behaviour": {
"color": "黑色",
"size": "M",
"isNew": true
}
},
{
"userId": "33",
"otherId": "c",
"key1": "本田",
"key2": "CRV",
"behaviour": {
"color": "绿色",
"size": "L"
}
}]
以下是我拥有的 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;
}
我需要获取 MainObject 列表以进行进一步处理。尝试了以下代码,但不确定如何映射 behavior 集合 -
String inputLine = currentBufferedReader.readLine();
// 以上字符串包含完整的 JSON
ObjectMapper objectMapper = new ObjectMapper();
MainObject[] mainObjects = objectMapper.readValue(inputLine, MainObject[].class);
我得到:Unrecognized field not marked as ignorable with above code. 请提供建议。
英文:
I need help in mapping below JSON structure for further processing.
[{
"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:
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. 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.
答案1
得分: 1
定义您的类如下所示:
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 + "]";
}
}
映射的代码:
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));
}
输出:
[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}]]
英文:
Define ur class like 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}]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论