[英]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 : 我试图解析一个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
},
the problem in question is the "runtime" 问题在于“运行时”
the cause of problem is : "java lang NumberFormatException : Invalid double : "" 问题的原因是:“ java lang NumberFormatException:无效的double:”
and you sure know that with Gson you need to create a class that goes like this : 并且您肯定知道,使用Gson,您需要创建一个像这样的类:
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. 我该如何欺骗程序,知道它不是我的API。
By changing private int runtime;
通过更改
private int runtime;
to private String runtime;
到
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. 根据您随后执行的操作,您可以检查它是否为int,如果为int,则将其作为int处理,否则将其作为String处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.