GSON解析android
这是我的 JSON:
{
"Curs": [
{
"ID": "AED",
"Name": "Dirhamul Emiratelor Arabe",
"Curs": "0.8503",
"Multiplier": "1",
"Data": "2011-02-09"
},
{
"ID": "AUD",
"Name": "Dolarul australian",
"Curs": "3.1572",
"Multiplier": "1",
"Data": "2011-02-09"
}
]
}
这是我的类:
public class Curs {
@SerializedName("ID")
public String id;
@SerializedName("Name")
public String nume;
@SerializedName("Curs")
public double crs;
@SerializedName("Multiplier")
public int mult;
@SerializedName("Data")
public Date data;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNume() {
return nume;
}
public void setNume(String nume) {
this.nume= nume;
}
public void setCrs(double crs) {
this.crs=crs;
}
public double getCrs(){
return crs;
}
public void setMult(int mult) {
this.mult=mult;
}
public int getMult(){
return mult;
}
public void setData(Date data) {
this.data=data;
}
public Date getData(){
return data;
}
}
这是包含我的列表的类:
public class CursList {
public static List<Curs> listaCurs;
public void setCursList(List <Curs> listaCurs) {
this.listaCurs = listaCurs;
}
public List<Curs> getCursList() {
return listaCurs;
}
}
我像这样进行解析:
Gson gson=new Gson();
CursList listaCurs=gson.fromJson(Jsonresult,CursList.class);
当我访问列表时,我得到一个空指针。我做错了什么?
This is my JSON:
{
"Curs": [
{
"ID": "AED",
"Name": "Dirhamul Emiratelor Arabe",
"Curs": "0.8503",
"Multiplier": "1",
"Data": "2011-02-09"
},
{
"ID": "AUD",
"Name": "Dolarul australian",
"Curs": "3.1572",
"Multiplier": "1",
"Data": "2011-02-09"
}
]
}
This is my class:
public class Curs {
@SerializedName("ID")
public String id;
@SerializedName("Name")
public String nume;
@SerializedName("Curs")
public double crs;
@SerializedName("Multiplier")
public int mult;
@SerializedName("Data")
public Date data;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNume() {
return nume;
}
public void setNume(String nume) {
this.nume= nume;
}
public void setCrs(double crs) {
this.crs=crs;
}
public double getCrs(){
return crs;
}
public void setMult(int mult) {
this.mult=mult;
}
public int getMult(){
return mult;
}
public void setData(Date data) {
this.data=data;
}
public Date getData(){
return data;
}
}
And this is the class that contains my list:
public class CursList {
public static List<Curs> listaCurs;
public void setCursList(List <Curs> listaCurs) {
this.listaCurs = listaCurs;
}
public List<Curs> getCursList() {
return listaCurs;
}
}
I do the parsing like this:
Gson gson=new Gson();
CursList listaCurs=gson.fromJson(Jsonresult,CursList.class);
When i access the list i get a null pointer.What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发现了愚蠢的问题。 Json 不接受日期格式。这就是问题所在。感谢大家的帮助。
Found the stupid problem . Json doesn't accept Date format . That was the problem. Thanks to all for the help.
Gson 试图将列表放入名为“Curs”的字段中,但 CursList 类中没有这样的字段。您应该在 CursList 中添加注释,例如:
Gson is attempting to put the list into a field called "Curs", but there is no such field inside the CursList class. You should add an annotation in CursList like: