1

I am calling a service which returns responses as json and turn them into a Java object using Gson. In 47 of 50 cases, I get the Java object however in 3 of the cases, I get the following error:

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)

One json response which is failing can be seen here: http://ec2-50-112-212-186.us-west-2.compute.amazonaws.com/ske.json. My Java bean is the following:

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;
        }
    }
}

and the actual gson call is gson.fromJson(json, WordSketch.class).

Could anyone point out what is wrong or at least give me some light as to how to debug (quickly) since I don't know in which part of the json, gson is failing?

Thanks a lot,

1 Answer 1

4

By observing your JSON, I've realised that some fields that usually have a numeric value, other times they have an empty string as value.

For example, the 3rd gramrel in the response is:

{
    "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
},

As in your class you have defined count and score of type Number, GSON complains!


EDIT: I suggest you to have all fields in your classes to be of type String and then you can convert it as you need... I mean, you can use the class you wrote just as a response wrap class, and then create your actual classes from that response class...

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, ...);
}

In this example, your Gramrel class is only a temporary class to wrap the response, and MyGramrel is the actual class you'll use (the one with the properties of type Number)....

Sign up to request clarification or add additional context in comments.

2 Comments

hmm ic. 10x! best to change everything to string then I guess and then parse to a number?
exactly! it's just what I was writing in the EDIT... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.