简体   繁体   中英

Gson parsing with a multiple return type json object

I'm trying to parse a json object that is int when it's good, and string when it's not, here's an example :

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
},

the problem in question is the "runtime"

the cause of problem is : "java lang NumberFormatException : Invalid double : ""

and you sure know that with Gson you need to create a class that goes like this :

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

public int getRuntime() {

        return runtime;
    }

}

How can i trick the program, know that it's not my API.

By changing private int runtime; to private String runtime;

Depending what you do afterwards, you can check if its a int, if it is , deal with it as int, else deal with it as String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM