gpt4 book ai didi

java - 无法反序列化 JSON 响应

转载 作者:行者123 更新时间:2023-12-02 04:32:58 25 4
gpt4 key购买 nike

如何使用 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

如何克服这个问题。

最佳答案

您需要一个类来匹配时区数组

public class TimeZoneClass {
private Object[] tz_values ;
}

并且在

改变这个

private String[] tzs;

到此

private TimeZoneClass[] tzs;

关于java - 无法反序列化 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com