【问题标题】:Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at异常:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例
【发布时间】:2019-01-15 12:27:56
【问题描述】:

我正在尝试显示来自 Web 服务的数据并收到此错误: 线程“main”com.fasterxml.jackson.databind.JsonMappingException 中的异常:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 实例...

在我的代码下面找到:

模型对象:

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

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

谢谢

【问题讨论】:

  • 你想先阅读响应然后使用对象映射器吗?同样作为回应,在城镇内部,您需要创建具有公社数组的类,然后使用 parse

标签: java json


【解决方案1】:

您收到此错误是因为您尝试将实际上不是 JSON Array 的内容反序列化为 Collection

如果您能够将 JSON 更改为仅发送 JSON Array 格式,它将如下所示:

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

否则,要正确解析它,您还可以创建一个新类来表示 JSON 的 town 并使用它进行解析:

public class TownRecords implements Serializable {

    private List<Commune> communes;

    ... getters and setters
}

【讨论】:

    【解决方案2】:

    您正在尝试反序列化 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);
    

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2015-09-21
      • 2021-03-26
      • 2017-09-22
      • 2020-02-20
      • 2018-02-03
      • 2019-10-16
      相关资源
      最近更新 更多