我打電話給一個服務,它將響應作爲json返回,並使用Gson將它們轉換爲Java對象。在47 50的情況下,我在的情況下,3但是獲得Java對象,我得到以下錯誤:這是失敗可以看到這裏解析json到java對象時出錯:期望編號,得到字符串
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失敗?
非常感謝,
hmm ic。 10倍!最好把所有東西都改成string然後我猜,然後解析成一個數字?確切地說, –
!這正是我在編輯中寫的東西...... :) – MikO