【发布时间】:2016-03-04 12:19:04
【问题描述】:
我有以下 JSON 文件:
{
“weight": {
"type": "weight",
"range": [
"2016-02-15",
"2016-02-16",
"2016-02-17",
"2016-02-18",
"2016-02-19",
"2016-02-20",
"2016-02-21"
],
"days": [
{
"weight": [
{
"bmi": 29.5,
"date": "2016-02-14",
"logId": 1455494399000,
"source": "API",
"time": "23:59:59",
"weight": 90.3
},
]
}
然后,我希望将此 JSON 添加到以下类。
public class fitbitTypeWeight {
public fitbitDays weight;
}
public class fitbitDays {
public fitbitDayWeight days;
}
public class fitbitDayWeight {
public fitbitWeight weight;
}
public class fitbitDayWeight {
public fitbitWeight weight;
}
然后,我有下面的代码来尝试解析它。
public static void readJSON() {
Gson gson = new Gson();
type = gson.fromJson(file, fitbitTypeWeight.class);
GsonBuilder builder = new GsonBuilder();
gson = builder.create();
createFitBitInfo();
}
private static void createFitBitInfo() throws ITrustException{
RemoteMonitoringDAO db = new RemoteMonitoringDAO(prodDAO);
RemoteMonitoringDataBean temp=new RemoteMonitoringDataBean();
fitbitDayWeight info = type.weight.days;
temp.setFitbitWeight(info.weight.weight);
temp.setFitbitDate(info.weight.date);
temp.setLoggedInMID(userID);
db.storeFitbitData(temp);
}
但是,我在 fitbitDayWeight info = type.weight.days; 上遇到 NPE 异常;
对可能出现的问题有什么建议吗?
【问题讨论】:
标签: java json nullpointerexception gson