嗨,我正在尝试读取以下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 [] []疾病仍然无法解析,请让我知道我在这里想念的是什么
解析代码
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:无法反序列化以下实例
com.goodyzain.bern.models.BernOBJ
不在START_ARRAY令牌的[来源:(String)“ [{”代名词“:[{” id“:[” CUI-less“],” obj“:”疾病“,” span“:{”开始“:31 ,“ end”:41}}],“ elapsed_time”:{“ ner”:2.759,“ normalization”:0.002,“ tmtool”:0.148,“ total”:2.91},“ logits”:{“疾病”:[ “ [截断了555个字符];第1行,第1列,位于com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)〜com.fasterxml的[jackson-databind-2.11.1.jar:2.11.1]。 jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1468)〜[jackson-databind-2.11.1.jar:2.11.1] at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext。
您当前遇到的错误:
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;
private Double numericValue;
}
然后将您的List<List<Disease>>
字段类型更改为List<DiseasePair>
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句