如何使用java pojo类反序列化下面的json响应。
JSON响应
"details":
{
"distance": 14745,
"km": 14.745,
"steps": 16804,
"active_time": 11927,
"longest_active": 2516,
"inactive_time": 32760,
"longest_idle": 27180,
"calories": 1760.30480012,
"bmr_day": 1697.47946931,
"bmr": 1697.47946931,
"bg_calories": 1099.9439497,
"wo_calories": 388.506116077,
"wo_time": 11484,
"wo_active_time": 3902,
"wo_count": 2,
"wo_longest": 2516,
"tz": "America/Los Angeles",
"tzs":
[
[1384963500, "America/Phoenix"],
[1385055720, "America/Los_Angeles"]
]
}
我已经编写了下面的pojo catch用于反序列化JSON响应。
Response.java
public class Response {
private Distance distance;
}
Distance.java
public class Distance{ private int distance;
private double km;
private int steps;
private int active_time;
private int longest_active;
private int inactive_time;
private int longest_idle;
private int calories;
private int bmr_day;
private int bmr;
private int bg_calories;
private int wo_calories;
private int wo_time;
private int wo_active_time;
private int wo_count;
private int wo_longest;
private int sunrise;
private int sunset;
private String tz;
private String[] tzs;
private String hourly_totals;
}
我收到以下异常,
Can not deserialize instance of java.lang.String out of START_ARRAY token
atSource: java.io.PushbackInputStream@e6819e;(through reference chain: Details["tzs"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of java.lang.String out of START_ARRAY token
如何克服这一点。
答案 0 :(得分:2)
您需要一个类来匹配时区数组
public class TimeZoneClass {
private Object[] tz_values ;
}
并在
更改此
private String[] tzs;
到这个
private TimeZoneClass[] tzs;