例外:无法在 START_OBJECT 标记之外反序列化 java.util.ArrayList 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54198875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at
提问by FooBayo
I am trying to display data from a web service and getting this error : Exception in thread "main" com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at...
我正在尝试显示来自 Web 服务的数据并收到此错误: 线程“main”com.fasterxml.Hymanson.databind.JsonMappingException 中的异常:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例...
Find below my code :
在我的代码下面找到:
Model object :
模型对象:
public class Commune implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Commune [name=" + name + "]";
}
}
Main class:
主要类:
public class Test {
public static void main(String[] args) throws IOException {
URL url = new URL("https://bj-decoupage-territorial.herokuapp.com/api/v1/towns");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
if(connection.getResponseCode() != 200){
throw new RuntimeException("Failed : Http code : "+connection.getResponseCode());
}
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String output;
while((output = reader.readLine()) != null){
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<Commune>> mapType = new TypeReference<List<Commune>>() {};
List<Commune> jsonToCommuneList = mapper.readValue(output, mapType);
for(Commune c : jsonToCommuneList) {
System.out.println(c.getName());
}
}
connection.disconnect();
}
}
Here is the data getting from the url :
这是从 url 获取的数据:
{"towns":
[
{"name":"BANIKOARA"},
{"name":"GOGOUNOU"},
{"name":"KANDI"},
{"name":"KARIMAMA"}
]
}
Help me please to check what i did wrong.
请帮助我检查我做错了什么。
Thank you
谢谢
采纳答案by kami
You're trying to deserialize a list of Commune, but in HTTP response you're getting an object, containing such a list, but not a list itself. So, you need another wrapper object for deserialisation:
您正在尝试反序列化 Commune 列表,但在 HTTP 响应中,您得到一个对象,其中包含这样一个列表,但不是列表本身。因此,您需要另一个用于反序列化的包装器对象:
Wrapper
包装器
public class Towns implements Serializable {
private List<Commune> towns;
public Towns() {}
public List<Commune> getTowns() {
return towns;
}
public void setTowns(List<Commune> towns) {
this.towns = towns;
}
@Override
public String toString() {
return "Towns: " + towns.toString();
}
}
main app
主应用程序
TypeReference<Towns> mapType = new TypeReference<Towns>() {};
Towns towns = mapper.readValue(output, mapType);
System.out.println(towns);
回答by nullptr
You are getting this error because you are trying to deserialize something that is not actually a JSON Array
into a Collection
您收到此错误是因为您正在尝试将实际上不是 a 的内容反序列JSON Array
化为Collection
If you are able to change your JSON to send just a JSON Array
format, it will look like this one below:
如果您能够将 JSON 更改为仅发送一种JSON Array
格式,它将如下所示:
[
{"name":"BANIKOARA"},
{"name":"GOGOUNOU"},
{"name":"KANDI"},
{"name":"KARIMAMA"}
]
Otherwise, to parse it correctly you also can create a new class which will represent the town
of your JSON and parse using it:
否则,要正确解析它,您还可以创建一个新类,该类将代表town
您的 JSON 并使用它进行解析:
public class TownRecords implements Serializable {
private List<Commune> communes;
... getters and setters
}