简体   繁体   中英

GSON : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING

I am facing a problem about the GSON json to Java. I looked up many posts here, but I cannot find the solution for my question. So I list my problem here. Thanks very much !

Here is my Json Data

"{
    "data": {
        "marks": "",
        "spines": "",
        "dendrites": {
            "voxel": [
                [383,382,382,381],
                [49,50,51,52],
                [7,10,10,10],
                [0,0,0,0]
            ],
            "maxint": [32,42,28,28],
            "maxintcorrected": null,
            "maxintcube": null,
            "medianfiltered": [54.36979745,54.36979745,54.36979745,54.36979745],
            "meanbacksingle": null,
            "maxintsingle": null,
            "thres": null,
            "meanback": 42,
            "index": 1,
            "length": [0,0.3223757885,0.6336712814,0.9350672197,1.227262867],
            "XYlength": [0,0.2085982964,0.410997367,0.6084466603]
        }
    }
}
"

My Class's defination is following

public class Test {
    public data data;

    public class data {

        public Object marks;
        public String spines = "";
        public StandardSpinenalysisImage.data.dendrites[] dendrites;

        public class dendrites {

            public int voxel[][];
            public int maxint[];
            public String maxintcorrected = "";
            public String maxintcube = "";
            public int medianfiltered[];
            public String meanbacksingle = "";
            public String maxintsingle = "";
            public int thres = 0;
            public int meanback;
            public int index = 0;
            public int length[];
            public int XYlength[];

            public dendrites() {
                this.thres = 100;
            }
        }
    }
}

But when I use the code Test t = g.fromJson(input, Test.class);

I got the error result.

Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5

Thank you very very MUCH!!

your json looks good. just try remove the " from the beginning and the end of the json

you can try using this link to verify your json

Faced the same error today. In mine, the problem was one variable in JSON mapped to a String, but in the class the variable was on object of a Class Type. This made Gson think that the JSON variable should be of the Class Type, causing IllegalStateException with Expected BEGIN_OBJECT but was STRING

Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM