我的自定义课程中的数据无法显示。
我从php脚本获取我的JSON对象并将其存储在字符串result
中,然后我尝试将其插入我的自定义类Planets
从该类我通过get方法调用特定变量,但它没有在我的TextView result_tv
中显示
这是代码:
String jsonString = result;
Gson gson = new Gson();
Planets seznamPlanet = gson.fromJson(jsonString, Planets.class);
this.result_tv.setText(seznamPlanet.getPlanetName());
//this.result_tv.setText(jsonString); this works
我的课程是:
public class Planets {
public String name;
public Integer size;
public Integer o2;
public Integer atmosphere;
public String getPlanetName() {
return name;
}
public Integer getPlanetSize() {
return size;
}
public Integer getPlanetO2() {
return o2;
}
public Integer getPlanetAtmosphere() {
return atmosphere;
}
}
结果字符串中的和JSON对象如下所示:
{"Planets":[{"name":"Earth","size":"3","o2":"1","atmosphere":"1"},{"name":"Mars","size":"8","o2":"0","atmosphere":"0"}]}
答案 0 :(得分:0)
将您的Planets
课程重命名为Planet
(以避免冲突)
public class Planet {
public String name;
public Integer size;
public Integer o2;
public Integer atmosphere;
}
创建另一个名为PlanetsList
public class PlanetsList {
List<Planet> Planets; //rename was useful so we don't have List<Planets> Planets
}
然后像这样调用gson:
PlanetsList seznamPlanet = gson.fromJson(jsonString, PlanetsList.class);
答案 1 :(得分:0)
尝试这样的事情:
public class Planets {
public Planet[] planets;
@Override
public String toString() {
return (new Gson().toJson(this));
}
public final class Planet {
public String name;
public Integer size;
public Integer o2;
public Integer atmosphere;
@Override
public String toString() {
return (new Gson().toJson(this));
}
}
}