JSON 反序列化抛出异常 - 无法从 START_OBJECT 令牌反序列化 java.util.ArrayList 的实例 [英] JSON deserialization throwing exception - Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

查看:271
本文介绍了JSON 反序列化抛出异常 - 无法从 START_OBJECT 令牌反序列化 java.util.ArrayList 的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的 JSON 响应,

The below is my JSON response,

引起:com.fasterxml.jackson.databind.JsonMappingException:无法从 START_OBJECT 令牌反序列化 java.util.ArrayList 的实例在 [来源:java.io.PushbackInputStream@bce1d9;行:1,列:556](通过参考链:com.totalHours["data"]->com.totalHours["hourly_totals"])

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.PushbackInputStream@bce1d9; line: 1, column: 556] (through reference chain: com.totalHours["data"]->com.totalHours["hourly_totals"])

 "totalHours": 
  {
     "hourly_totals": 
     {
        "2013112101":
        {
           "distance": 1324,
           "calories": 90.0120018125,
           "steps": 1603,
           "active_time": 793,
           "inactive_time": 220,
           "longest_active_time": 302,
           "longest_idle_time": 780
        },
        "2013112101":
        {
           "distance": 626,
           "calories": 47.0120018125,
           "steps": 455,
           "active_time": 246,
           "inactive_time": 260,
           "longest_active_time": 203,
           "longest_idle_time": 650
        },
        ... more hours ...
     }  

我参加了如下的 pojo 课程,当我尝试反序列化我的 json 数据时出现异常.

I have took a pojo class like below, I am getting an exception when I try to deserialize the my json data.

public class totalHours{
private List<String> hourly_totals;
}

但是,我不知道要不要取List,因为没有数组响应.还有哪些可以尝试的选项.

But, I don't know whether to take a List because there is no array in response. What will be the other options to try out for.

推荐答案

hourly_totals 是一个对象从一个小时表示为一个字符串到另一个对象,到Map ,其中 T 是表示一小时数据的 POJO 类,看起来是一个自然的选择.

hourly_totals is an object from an hour represented as a string to another object, to Map<String,T> , where T is a POJO class representing the data for one hour, would look like a natural choice.

这篇关于JSON 反序列化抛出异常 - 无法从 START_OBJECT 令牌反序列化 java.util.ArrayList 的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆