我正在调用一个服务,它将响应作为json返回,并使用Gson将它们转换为Java对象。在50个案例中的47个案例中,我得到了Java对象,但在其中3个案例中,我收到以下错误:
com.google.gson.JsonSyntaxException: Expecting number, got: STRING
at com.google.gson.internal.bind.TypeAdapters$11.read(TypeAdapters.java:304)
at com.google.gson.internal.bind.TypeAdapters$11.read(TypeAdapters.java:293)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
在这里可以看到一个失败的json响应:http://ec2-50-112-212-186.us-west-2.compute.amazonaws.com/ske.json。我的Java bean如下:
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class WordSketch{
@SerializedName("Gramrels")
private List<Gramrel> gramrels;
public List<Gramrel> getGramrels(){
return this.gramrels;
}
public void setGramrels(List<Gramrel> gramrels){
this.gramrels = gramrels;
}
public static class Gramrel {
@SerializedName("Words")
private List<Word> words;
private Number count;
private String name;
private Number score;
private Number seek;
public List<Word> getWords(){
return this.words;
}
public void setWords(List<Word> words){
this.words = words;
}
public Number getCount(){
return this.count;
}
public void setCount(Number count){
this.count = count;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Number getScore(){
return this.score;
}
public void setScore(Number score){
this.score = score;
}
public Number getSeek(){
return this.seek;
}
public void setSeek(Number seek){
this.seek = seek;
}
}
public static class Word {
private Number count;
private Number id;
private String lempos;
private Number score;
private Number seek;
private String word;
public Number getCount(){
return this.count;
}
public void setCount(Number count){
this.count = count;
}
public Number getId(){
return this.id;
}
public void setId(Number id){
this.id = id;
}
public String getLempos(){
return this.lempos;
}
public void setLempos(String lempos){
this.lempos = lempos;
}
public Number getScore(){
return this.score;
}
public void setScore(Number score){
this.score = score;
}
public Number getSeek(){
return this.seek;
}
public void setSeek(Number seek){
this.seek = seek;
}
public String getWord(){
return this.word;
}
public void setWord(String word){
this.word = word;
}
}
}
,实际的gson调用是gson.fromJson(json, WordSketch.class)
。
任何人都可以指出什么是错的,或者至少让我对如何调试(快速)有所了解,因为我不知道json的哪个部分,gson失败了?
非常感谢,
答案 0 :(得分:4)
通过观察你的JSON,我意识到一些字段通常有一个数值,有时候它们有一个空字符串作为值。
例如,响应中的第三个 gramrel 是:
{
"count": "",
"colbreak": 1,
"name": "unary rels",
"score": "",
"Words": [
{
"count": 6,
"word": "prp_għaċ-",
"name": "prp_għaċ-",
"score": 9.1,
"Words": [ ],
"seek": 2231297
}
],
"seek": 0
},
在课堂上,您定义了count
和score
类型Number
,GSON会抱怨!
编辑:我建议您让班级中的所有字段都为String
类型,然后您可以根据需要进行转换...我的意思是,您可以使用您只是作为响应包类编写的类,然后从该响应类创建实际的类...
WordSketch ws = gson.fromJson(json, WordSketch.class);
for (Gramrel g in ws.getGramrels) {
Number count = //convert the String into Number...
//etc...
MyGrammel myGrammel = new MyGrammel(count, ...);
}
在此示例中,您的Gramrel
类只是包装响应的临时类,MyGramrel
是您将使用的实际类(具有类型Number
)的属性....