GSON:java.lang.IllegalStateException:预期为 BEGIN_OBJECT 但为 STRING

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12276979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:17:47  来源:igfitidea点击:

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

javajsongson

提问by Lawrence

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 !

我面临关于 GSON json 到 Java 的问题。我在这里查了很多帖子,但找不到我的问题的解决方案。所以我在这里列出我的问题。非常感谢 !

Here is my Json Data

这是我的 Json 数据

"{
    "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);

但是当我使用代码 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!!

非常非常感谢你!!

回答by austin

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

你的 json 看起来不错。只需尝试从json的开头和结尾删除“

you can try using this linkto verify your json

您可以尝试使用此链接来验证您的 json

回答by kushpf

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

今天遇到同样的错误。在我的问题中,问题是 JSON 中的一个变量映射到一个字符串,但在类中,该变量位于类类型的对象上。这让 Gson 认为 JSON 变量应该是 Class Type,导致 IllegalStateExceptionExpected BEGIN_OBJECT but was STRING

Hope it helps someone.

希望它可以帮助某人。