我正在调用一个服务,它将响应作为 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 失败了?
非常感谢,