异常:无法将START_OBJECT令牌的实例反序列化为java.util.ArrayList。

7
我是一个有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我正在尝试从 Web 服务中显示数据,但出现以下错误:异常在线程 "main" 中:"com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at..."

以下是我的代码:

模型对象:

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 + "]";
    }
}

主类:

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();        
    }
}

这里是从URL获取的数据:

{"towns":
   [
      {"name":"BANIKOARA"},
      {"name":"GOGOUNOU"},
      {"name":"KANDI"},
      {"name":"KARIMAMA"}
   ]
}

请帮我检查一下我做错了什么。

谢谢


可能是 https://dev59.com/VIHba4cB1Zd3GeqPRXXg 的重复问题。 - Jabongg
你想先读取响应,然后再使用对象映射器吗?另外,在响应中,它位于towns内,您需要创建一个具有Commune数组的类,然后使用parse。 - ygbgames
2个回答

10
您收到此错误是因为您试图将实际上不是 JSON数组 的内容反序列化为 Collection
如果您能够更改您的JSON以仅发送 JSON数组 格式,它会像下面这个示例一样:
[
  {"name":"BANIKOARA"},
  {"name":"GOGOUNOU"},
  {"name":"KANDI"},
  {"name":"KARIMAMA"}
]
否则,为了正确解析它,您还可以创建一个新类,表示您的JSON中的town,并使用它进行解析:
public class TownRecords implements Serializable {

    private List<Commune> communes;

    ... getters and setters
}

5
你正在尝试反序列化一个Commune列表,但在HTTP响应中你得到的是一个包含这样一个列表的对象,而不是列表本身。因此,你需要另一个包装器对象进行反序列化: 包装器
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();
    }
}

主应用程序

    TypeReference<Towns> mapType = new TypeReference<Towns>() {};
    Towns towns = mapper.readValue(output, mapType);
    System.out.println(towns);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接