【问题标题】:GSON : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRINGGSON:java.lang.IllegalStateException:应为 BEGIN_OBJECT 但为 STRING
【发布时间】:2012-08-29 21:50:01
【问题描述】:

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

这是我的 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]
        }
    }
}
"

我的班级的定义如下

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

但是当我使用代码时 测试 t = g.fromJson(input, Test.class);

我得到了错误结果。

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

非常感谢!!

【问题讨论】:

  • “marks”是“Object”类型而其他都是“String”类型的原因吗?
  • 为什么你的 json 文件的开头和结尾都有'"'?
  • @JasonSperske 我只是测试它......我认为标记在这里也是一种字符串。测试一下
  • @giorashc。你好谢谢。我只是从调试模式复制它。它说 Expected BEGIN_OBJECT 但在第 1 行第 5 列是 STRING。问题出在哪里?谢谢
  • Java 中的类名应以大写字母开头:错误:public class dendrites good:public class Dendrites

标签: java json gson


【解决方案1】:

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

您可以尝试使用this link 来验证您的json

【讨论】:

  • JSon 没问题。 " 用来告诉它一个字符串.. String json="that above json";
【解决方案2】:

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

希望对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2016-06-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多