0

我有这种 JSON 格式:

["com.atlassian.greenhopper.service.sprint.Sprint@60d1cf92[id=636,rapidViewId=69,state=CLOSED,name=ABC-1,startDate=2016-07-18T08:22:00.000-04:00,endDate=2016-07-29T04:15:00.000-04:00,completeDate=2016-08-09T10:34:24.009-04:00,sequence=636]", "com.atlassian.greenhopper.service.sprint.Sprint@461fc487[id=656,rapidViewId=69,state=ACTIVE,name=ABC-2,startDate=2016-08-09T10:42:41.342-04:00,endDate=2016-08-19T06:35:00.000-04:00,completeDate=<null>,sequence=656]"]

我正在尝试使用 Gson 解析来解析它,但是得到了这个Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $[0].

用于解析的 Java 片段和 Spring bean 如下:

Type sprintBeanType = new TypeToken<List<SprintBean>>() {}.getType();
List<SprintBean> sprintBeanList = gson.fromJson(json, sprintBeanType);

public class SprintBean{
    @Expose
    private String sprint;

    public String getSprint() {
        return sprint;
    }

    public void setSprint(String sprint) {
        this.sprint = sprint;
    }

}

非常感谢解析此 JSON 的任何帮助。

4

1 回答 1

0

您的 JSON 数据看起来只是一个字符串列表,因此可以List<String>通过将类型 generic 和 list var type 替换为来将其解析为List<String>

Type sprintStringType = new TypeToken<List<String>>() {}.getType();
List<String> sprintStringList = gson.fromJson(json, sprintStringType);

但是,这只会将基本的 JSON 数据解析为字符串,不会解析“内部”数据。此外,您不太可能使用 GSON 解析每个字符串,因为它不是有效的 JSON 数据。

于 2016-12-03T22:10:14.590 回答