我正在尝试反序列化一个包含数组和多个级别的JSON字符串。我只想要“attributes”属性和“id”的Map。下面是一个示例:
{
"data": [
{
"id": "3252",
"type": "job_seekers",
"attributes": {
"first_name": "nathan",
"last_name": "taylor",
"full_name": "nathan taylor",
"email": "",
"phone": "",
"phone_iso2": "CA",
"date_of_birth": null,
"summary": "",
"created_at": "2023-01-03T17:34:57+00:00",
"updated_at": "2023-04-21T19:16:05+00:00",
"locale": "en",
"channel": "web_traffic_referral",
"channel_source": "",
"pipeline_source": "self_registered",
"address1": null,
"address2": null,
"town": null,
"country": null,
"postcode": null
},
"relationships": {
"answers": {
"meta": {
"included": false
}
},
"custom_attributes": {
"meta": {
"included": false
}
},
"comments": {
"meta": {
"included": false
}
}
}
},
{
"id": "26346",
"type": "job_seekers",
"attributes": {
"first_name": "spencer",
"last_name": "gordon",
"full_name": "spencer gordon",
"email": "",
"phone": "",
"phone_iso2": "US",
"date_of_birth": null,
"summary": "",
"created_at": "2023-01-06T21:52:40+00:00",
"updated_at": "2023-04-21T19:16:43+00:00",
"locale": "en",
"channel": "direct",
"channel_source": null,
"pipeline_source": "self_registered",
"address1": null,
"address2": null,
"town": null,
"country": null,
"postcode": null
},
"relationships": {
"answers": {
"meta": {
"included": false
}
},
"custom_attributes": {
"meta": {
"included": false
}
},
"comments": {
"meta": {
"included": false
}
}
}
}
],
"meta": {}
}
这是我尝试过的:
ObjectMapper objectMapper = new ObjectMapper();
List<JobSeeker> myObjects = objectMapper.readValue(EntityUtils.toString(entity), new TypeReference<List<JobSeeker>>(){});
for (JobSeeker itr : myObjects)
{
System.out.println(itr);
}
错误代码:
SEVERE: null
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<app.JobSeeker>` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (StringReader); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1752)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1526)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1473)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:396)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:252)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4825)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3772)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3755)
at app.Main.main(Main.java:52)
1条答案
按热度按时间vc9ivgsu1#
你试图解析一个
List<JobSeeker>
,而你的JSON是一个 object,带有一个data
字段,* 包含 *(我认为是)一个List<JobSeeker>
。因此,创建另一个 Package 器对象并解析它:然后更改解析:
objectMapper.readValue(EntityUtils.toString(entity), new TypeReference<Data>(){});