英文:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance?
问题
以下是您的文本的翻译部分:
我已经尝试了一切来读取以下JSON字符串,但仍然收到以下错误。
我的JSON字符串是有效的,我认为问题是子元素与映射有问题。
这是我的JSON字符串:
[
{
"denotations": [
{
"id": [
"CUI-less"
],
"obj": "disease",
"span": {
"begin": 31,
"end": 41
}
}
],
"elapsed_time": {
"ner": 2.759,
"normalization": 0.002,
"tmtool": 0.148,
"total": 2.91
},
"logits": {
"disease": [
[
{
"end": 41,
"id": "CUI-less",
"start": 31
},
0.999957799911499
]
],
"drug": [],
"gene": [],
"species": []
},
"project": "BERN",
"sourcedb": "PubMed",
"sourceid": "2832773",
"text": "Absence of humoral immunity to poliovirus in vaccinated individuals.",
"timestamp": "Thu Aug 06 13:42:27 +0000 2020"
}
]
我的Java POJO(Logit类)
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"disease",
"drug",
"gene",
"species"
})
public class Logits {
@JsonProperty("disease")
private List<List<Disease>> disease = null;
@JsonProperty("drug")
private List<Object> drug = null;
@JsonProperty("gene")
private List<Object> gene = null;
@JsonProperty("species")
private List<Object> species = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("disease")
public List<List<Disease>> getDisease() {
return disease;
}
@JsonProperty("disease")
public void setDisease(List<List<Disease>> disease) {
this.disease = disease;
}
@JsonProperty("drug")
public List<Object> getDrug() {
return drug;
}
@JsonProperty("drug")
public void setDrug(List<Object> drug) {
this.drug = drug;
}
@JsonProperty("gene")
public List<Object> getGene() {
return gene;
}
@JsonProperty("gene")
public void setGene(List<Object> gene) {
this.gene = gene;
}
@JsonProperty("species")
public List<Object> getSpecies() {
return species;
}
@JsonProperty("species")
public void setSpecies(List<Object> species) {
this.species = species;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
我甚至尝试了`String[][] disease`,但仍然无法解析。
解析代码:
String denotations = restTemplate.getForObject("http://com/text", String.class);
System.out.println(denotations);
ObjectMapper mapper = new ObjectMapper();
BernOBJ denoObj = mapper.readValue(denotations, BernOBJ.class);
System.out.println(denoObj);
堆栈跟踪
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化`com.goodyzain.bern.models.BernOBJ`实例
源:(String)"[
{
"denotations": [
{
"id": [
"CUI-less"
],
"obj": "disease",
"span": {
"begin": 31,
"end": 41
}
}
],
"elapsed_time": {
"ner": 2.759,
"normalization": 0.002,
"tmtool": 0.148,
"total": 2.91
},
"logits": {
"disease": [
[
{
"end": 41,
"id": "CUI-less",
"start": 31
},
0.999957799911499
]
],
"drug": [],
"gene": [],
"species": []
},
"project": "BERN",
"sourcedb": "PubMed",
"sourceid": "2832773",
"text": "Absence of humoral immunity to poliovirus in vaccinated individuals.",
"timestamp": "Thu Aug 06 13:42:27 +0000 2020"
}
]"
请注意,一些HTML实体编码(如")已被翻译为双引号(")以匹配JSON格式。如果您需要更多帮助,请告诉我。
英文:
I have tried everything to read the following JSON string, but I still get the below error.
My JSON string is valid and I think the issue is that the sub element is having problem with the mapping.
Here is my JSON string:
[
{
"denotations": [
{
"id": [
"CUI-less"
],
"obj": "disease",
"span": {
"begin": 31,
"end": 41
}
}
],
"elapsed_time": {
"ner": 2.759,
"normalization": 0.002,
"tmtool": 0.148,
"total": 2.91
},
"logits": {
"disease": [
[
{
"end": 41,
"id": "CUI-less",
"start": 31
},
0.999957799911499
]
],
"drug": [],
"gene": [],
"species": []
},
"project": "BERN",
"sourcedb": "PubMed",
"sourceid": "2832773",
"text": "Absence of humoral immunity to poliovirus in vaccinated individuals.",
"timestamp": "Thu Aug 06 13:42:27 +0000 2020"
}
]
My Java POJO (Logit's class)
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"disease",
"drug",
"gene",
"species"
})
public class Logits {
@JsonProperty("disease")
private List<List<Disease>> disease = null;
@JsonProperty("drug")
private List<Object> drug = null;
@JsonProperty("gene")
private List<Object> gene = null;
@JsonProperty("species")
private List<Object> species = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("disease")
public List<List<Disease>> getDisease() {
return disease;
}
@JsonProperty("disease")
public void setDisease(List<List<Disease>> disease) {
this.disease = disease;
}
@JsonProperty("drug")
public List<Object> getDrug() {
return drug;
}
@JsonProperty("drug")
public void setDrug(List<Object> drug) {
this.drug = drug;
}
@JsonProperty("gene")
public List<Object> getGene() {
return gene;
}
@JsonProperty("gene")
public void setGene(List<Object> gene) {
this.gene = gene;
}
@JsonProperty("species")
public List<Object> getSpecies() {
return species;
}
@JsonProperty("species")
public void setSpecies(List<Object> species) {
this.species = species;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
I did even try String[][] disease
but it still fails to parse.
Parsing Code:
String denotations = restTemplate.getForObject("http://com/text",String.class);
System.out.println(denotations);
ObjectMapper mapper = new ObjectMapper();
BernOBJ denoObj = mapper.readValue(denotations, BernOBJ.class);
System.out.println(denoObj);
Stacktrace
> com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
> deserialize instance of com.goodyzain.bern.models.BernOBJ
out of
> START_ARRAY token at [Source: (String)"[
> {
> "denotations": [
> {
> "id": [
> "CUI-less"
> ],
> "obj": "disease",
> "span": {
> "begin": 31,
> "end": 41
> }
> }
> ],
> "elapsed_time": {
> "ner": 2.759,
> "normalization": 0.002,
> "tmtool": 0.148,
> "total": 2.91
> },
> "logits": {
> "disease": [
> "[truncated 555 chars]; line: 1, column: 1] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1468)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1242)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1190)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:604)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:190)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:166)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)
> ~[jackson-databind-2.11.1.jar:2.11.1] at
> com.goodyzain.bern.BernAPIController.getUsersById(BernAPIController.java:41)
> ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) ~[na:1.8.0_252] at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:1.8.0_252] at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:1.8.0_252] at java.lang.reflect.Method.invoke(Method.java:498)
> ~[na:1.8.0_252] at
> org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
> ~[tomcat-embed-core-9.0.37.jar:4.0.FR] at
> org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
> ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
> ~[tomcat-embed-core-9.0.37.jar:4.0.FR] at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
> ~[tomcat-embed-websocket-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
> ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> ~[tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> [na:1.8.0_252] at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> [na:1.8.0_252] at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> [tomcat-embed-core-9.0.37.jar:9.0.37] at
> java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]
答案1
得分: 5
当前的错误信息是:
Cannot deserialize instance of com.goodyzain.bern.models.BernOBJ out of START_ARRAY token at [Source: (String)"[ {
这意味着您有一个JSON对象数组(以[{...
开头),但尝试将其解析为单个对象:
BernOBJ denoObj = mapper.readValue(denotations, BernOBJ.class);
通常情况下,数组应该被解析为值的列表,例如:
List<BernOBJ> denoObj = mapper.readValue(denotations,
mapper.getTypeFactory().constructCollectionType(List.class, BernObj.class));
此外,您的List<List<Disease>>
列表并不完全包含Disease
对象,它包含一个对象和一个double
值:
[
[
{
"end": 41,
"id": "CUI-less",
"start": 31
},
0.999957799911499
]
]
我不确定那里是否总是一个对象和一个数值,以及它的含义,但您可以进行以下更改。创建一个额外的类,例如DiseasePair
,并将其映射如下:
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({ "disease", "numericValue" })
class DiseasePair {
private Disease disease;
private Double numericValue;
}
然后将您的List<List<Disease>>
字段类型更改为List<DiseasePair>
。
英文:
The current error that you have:
Cannot deserialize instance of com.goodyzain.bern.models.BernOBJ out of START_ARRAY token at [Source: (String)"[ {
means that you have a JSON array of objects (starting with [{...
), but try to parse it into a single object:
BernOBJ denoObj = mapper.readValue(denotations, BernOBJ.class);
An array is usually supposed to be parsed into a list of values, e.g. like this:
List<BernOBJ> denoObj = mapper.readValue(denotations,
mapper.getTypeFactory().constructCollectionType(List.class, BernObj.class));
Also, your List<List<Disease>>
list doesn't exactly contain Disease
objects, it contains an object and a double
value:
[
[
{
"end": 41,
"id": "CUI-less",
"start": 31
},
0.999957799911499
]
]
I'm not sure if there's always an object and a numeric value there, and what it means, but you could do the following. Create an additional class, e.g. DiseasePair
, and map it like this:
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({ "disease", "numericValue" })
class DiseasePair {
private Disease disease;
private Double numericValue;
}
Then change your List<List<Disease>>
field type to List<DiseasePair>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论