1

我正在尝试解析一个好的 json 对象,当它好的时候是 int ,而当它不是字符串时,这是一个例子:

id: "11271",
title: "Top Gun: An IMAX 3D Experience",
year: 1986,
mpaa_rating: "PG",
runtime: 110,
release_dates: {
theater: "2013-02-08",
dvd: "1998-10-20"
},
ratings: {
critics_rating: "Rotten",
critics_score: 50,
audience_rating: "Upright",
audience_score: 48
},


id: "771270981",
title: "Identity Thief",
year: 2013,
mpaa_rating: "R",
runtime: "",
release_dates: {
theater: "2013-02-08"
},
ratings: {
critics_score: -1,
audience_score: 97
},

有问题的问题是“运行时”

问题的原因是:“java lang NumberFormatException:Invalid double:”

你肯定知道使用 Gson 你需要创建一个像这样的类:

private int runtime;
public void setRuntime(int runtime) {
    this.runtime = runtime;
}

public int getRuntime() {

        return runtime;
    }

}

我怎么能欺骗程序,知道它不是我的 API。

4

1 回答 1

1

通过更改private int runtime;private String runtime;

根据您之后所做的事情,您可以检查它是否为 int,如果是,则将其作为 int 处理,否则将其作为 String 处理。

于 2013-01-28T19:19:13.077 回答