2015-07-03 152 views
0

如何使用java pojo類反序列化下面的json響應。無法反序列化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漁獲反序列化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: [email protected];(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

嘗試改變爲字符串[ ] tzs as List > – Raghavendra

+1

你不需要類TimeZone來匹配那部分JSON嗎? –

+1

您的JSON響應中的'tzs'不是字符串數組(而不是您的'Response'類),而是一個字符串數組數組。 –

回答

2

你需要一個類來匹配時區

public class TimeZoneClass { 
    private Object[] tz_values ; 
} 

而且在

變化的陣列此

private String[] tzs; 

這個

private TimeZoneClass[] tzs; 
+0

我試過上面但仍然是例外。 – Karthik

+1

我想你應該在TimeZoneClass中使用Object []而不是2個不同的字段...因爲只有'JSONObjects'會被解析成不同的字段......但是這裏是一個'JSONArray',它包含了ID和名字。 – Codebender

+0

@Codebender隨時編輯我的答案或張貼自己的,因爲你應得的信用;-) –