用不同的对象解析json数组到他们的类 [英] Parse json array with different objects to their classes
问题描述
我有以下json:
[
{
"type": 1,
"Steps": {
"steps": steps
}
},
{
"type": 2,
"HeartRate": {
"heartrates": heartRates
}
}
]
字段步骤只是一个int,heartRates是一个int的数组.我想要 使用gson将其解析为两个类. 我在stackoverflow上发现了类似的问题:如何解析多个json数组是gson的对象吗?并尝试了一下,但是没有用.这是我的代码:
The field steps is just an int an heartRates is an array of int's. I want to parse this using gson to two classes. I found a similar question on stackoverflow: How parse json array with multiple objects by gson? and tried it but it didn't work. This is my code:
public class DataModels extends ArrayList<DataModels.Container> {
public class Container {
public int type;
public Object object;
}
public class Steps {
double steps;
public Steps(double steps) {
this.steps = steps;
}
public double getSteps() {
return steps;
}
@Override
public String toString() {
return "Steps: " + steps;
}
}
public class HeartRate {
int heartRate;
public HeartRate(int hr) {
heartRate = hr;
}
public double getHeartRate() {
return heartRate;
}
@Override
public String toString() {
return "Heart rate: " + heartRate;
}
}
}
然后解析我的json:
Then for parsing my json:
public String getJSONMessage(String gearSData) {
System.out.println(gearSData);
Gson gson = new Gson();
DataModels model = gson.fromJson(gearSData, DataModels.class);
System.out.println(model);
for (DataModels.Container container: model) {
System.out.println(container.type);
System.out.println(container.object);
String innerJson = gson.toJson(container.object);
System.out.println("InnerJson: " + innerJson);
switch (container.type) {
case 1:
DataModels.Steps steps = gson.fromJson(innerJson, DataModels.Steps.class);
System.out.println(steps);
break;
case 2:
DataModels.HeartRate heartRate = gson.fromJson(innerJson, DataModels.HeartRate.class);
System.out.println(heartRate);
break;
}
}
}
该类型可以正确解析,但innerjson为null,我不知道为什么.有人可以解释一下吗,还是有人知道更好的方法?
The type gets parsed correctly but the innerjson is null and I don't know why. Can somebody explain this or does someone know a better way to do this?
推荐答案
您的字段名称应与json中的字段相同.我刚刚重命名了您的类的字段,并且您的代码对我来说很好用:
Names of your fields should be equal to the fields in json. I just renamed fields of your classes and your code works well for me:
public static String getJSONMessage(String gearSData) {
System.out.println(gearSData);
Gson gson = new Gson();
DataModels model = gson.fromJson(gearSData, DataModels.class);
System.out.println(model);
for (DataModels.Container container : model) {
System.out.println(container.type);
String innerJson = gson.toJson(container.type == 1 ? container.Steps : container.HeartRate);
System.out.println("InnerJson: " + innerJson);
//...
}
return null;
}
public static class DataModels extends ArrayList<DataModels.Container> {
public static class Container {
public int type;
public StepsType Steps; // object for type 1
public HeartRateType HeartRate; // object for type 2
}
public static class StepsType {
double steps;
//...
}
public static class HeartRateType {
int heartrates;
//...
}
}
这篇关于用不同的对象解析json数组到他们的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!