GSON解析android

发布于 2024-10-16 14:39:58 字数 1660 浏览 2 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春庭雪 2024-10-23 14:39:58

发现了愚蠢的问题。 Json 不接受日期格式。这就是问题所在。感谢大家的帮助。

Found the stupid problem . Json doesn't accept Date format . That was the problem. Thanks to all for the help.

红尘作伴 2024-10-23 14:39:58

Gson 试图将列表放入名为“Curs”的字段中,但 CursList 类中没有这样的字段。您应该在 CursList 中添加注释,例如:

public class CursList {
  @SerializedName("Curs")
  public static List<Curs> listaCurs;

  ...
}

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:

public class CursList {
  @SerializedName("Curs")
  public static List<Curs> listaCurs;

  ...
}
半葬歌 2024-10-23 14:39:58
 Gson gson=new Gson();
 Type cursType = new TypeToken<CursList>() { }.getType();
 CursList listaCurs=gson.fromJson(Jsonresult,cursType);
 Gson gson=new Gson();
 Type cursType = new TypeToken<CursList>() { }.getType();
 CursList listaCurs=gson.fromJson(Jsonresult,cursType);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文