[英]reading nested json with jackson - MismatchedInputException: Cannot deserialize value
stucked at accessing nested json.坚持访问嵌套的json。 similar stuff:类似的东西:
[
{
key-value,
key-value
},
{
key-value,
key-value
},
{
key-value,
key-value
}
]
works nicely but when i try:效果很好,但是当我尝试时:
{
"alfa":{
"id":"foo",
"product":{
"id":"foo1",
"price":"foo2"
}
},
"beta":{
"id":"foo",
"product":{
"id":"foo1",
"price":"foo2"
}
}
}
i get error:我得到错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<... com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化`java.util.ArrayList<...
i of course did change structure of classes:我当然确实改变了类的结构:
public class Alphabet{
private Purchase purchase;
...
public class Purchase{
private String id;
private Product product;
...
public class Product {
private String id;
private String price;
...
to read it:阅读它:
ObjectMapper mapper = new ObjectMapper();
InputStream inputStream = new FileInputStream(new File("src/main/json/file.json"));
TypeReference<List<Alphabet>> typeReference = new TypeReference<List<Alphabet>>() {};
List<Alphabet> alphabet= mapper.readValue(inputStream, typeReference);
System.out.println(alphabet);
whats wrong, please?请问怎么了?
您尝试读取的 JSON 结构似乎不是List<Alphabet>
,而是Map<String, Purchase>
。
Your second json not like list of object.你的第二个 json 不像对象列表。 The second json look lik have 2 main objects if so you need class like below.第二个json看起来有2个主要对象,如果你需要像下面这样的类。
public class Alphabet{
private Purchase purchase;
private Purchase purchase1;
}
But it not good practice.但这不是很好的做法。 Use as first josn like list of objects.用作第一个 josn 之类的对象列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.