1

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

非常感谢,

4

1 回答 1

4

通过观察您的 JSON,我意识到某些字段通常具有数值,有时它们具有空字符串作为值。

例如,响应中的第 3 个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
},

正如您在课堂上定义countscore类型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是您将使用的实际类(具有 type 属性的类Number)...。

于 2013-04-18T20:38:56.877 回答