GSon嵌套无名数组

时间:2018-09-11 20:17:42

标签: java json gson

我正在使用GSon解析我正在制作的游戏的纸牌列表,但是我似乎遇到了麻烦。我要解析的json是:

{
"name": "Core Set 2019",
"code": "M19",
"releaseDate": "2018-07-13",
"border": "black",
"type": "core",
"booster": [
  [
    "rare",
    "mythic rare"
  ],
  "uncommon",
  "uncommon",
  "uncommon",
  "common",
  "common",
  "common",
  "common",
  "common",
  "common",
  "common",
  "common",
  "common",
  "common",
  "land",
  "marketing"
]
}

问题出现在增强器部分。 netbeans给出的错误消息是

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:Expected a string but was BEGIN_ARRAY at line 9 column 8 path $.M19.booster[0]

到目前为止,我的代码是:

public class Set
{
    public String name;
    public String code;
    public String releaseDate;
    public String border;
    public String type;
    public List<String> booster;
    public Translation translations;
    public List<Card> cards;
}

主要是

Gson g = new GsonBuilder().create();
Set sets = g.fromJson(new FileReader(JSONNAME), Set.class);

如何解析数组对象中的无名数组?无名数组并不总是会存在,并且我无法更改json,因为它是从无法访问的站点下载的。

3 个答案:

答案 0 :(得分:1)

将您的课程更改为该课程:

public class JsonResponse
{
    private String name;
    private String code;
    private String releaseDate;
    private String border;
    private String type;
    ArrayList< Object > booster = new ArrayList < Object > ();


    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }

    public String getReleaseDate() {
        return releaseDate;
    }

    public String getBorder() {
        return border;
    }

    public String getType() {
        return type;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setReleaseDate(String releaseDate) {
        this.releaseDate = releaseDate;
    }

    public void setBorder(String border) {
        this.border = border;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "JsonResponse{" +
                "name='" + name + '\'' +
                ", code='" + code + '\'' +
                ", releaseDate='" + releaseDate + '\'' +
                ", border='" + border + '\'' +
                ", type='" + type + '\'' +
                ", booster=" + booster +
                '}';
    }
}

在我的情况下,我把它放在/resources/jsonexample.json上,所以我这样做是这样的:

Gson gson = new Gson();
        try {
            JsonResponse jsonResponse = gson.fromJson(new FileReader(new File("resources/jsonexample.json")), JsonResponse.class);
            System.out.println(jsonResponse.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

输出为

  

JsonResponse {name ='Core Set 2019',code ='M19',releaseDate ='2018-07-13',border ='black',type ='core',booster = [[稀有,神话,稀有] ,罕见,罕见,罕见,常见,常见,常见,常见,常见,常见,常见,常见,常见,常见,土地,市场营销]}

我已经创建了一个如何在Github上进行操作的存储库

答案 1 :(得分:1)

使用Object将起作用:

public class Set {
    public String name;
    public String code;
    public String releaseDate;
    public String border;
    public String type;
    public List<Object> booster;
}

public static void main(String[] args) {
    Gson gson = new Gson();
    Set set = gson.fromJson("{\n" +
            "\"name\": \"Core Set 2019\",\n" +
            "\"code\": \"M19\",\n" +
            "\"releaseDate\": \"2018-07-13\",\n" +
            "\"border\": \"black\",\n" +
            "\"type\": \"core\",\n" +
            "\"booster\": [\n" +
            "  [\n" +
            "    \"rare\",\n" +
            "    \"mythic rare\"\n" +
            "  ],\n" +
            "  \"uncommon\",\n" +
            "  \"uncommon\",\n" +
            "  \"uncommon\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"common\",\n" +
            "  \"land\",\n" +
            "  \"marketing\"\n" +
            "]\n" +
            "}", Set.class);

    for (Object o : set.booster) {
        System.out.println(o.getClass() + " " + o);
    }
}

输出:

class java.util.ArrayList [rare, mythic rare]
class java.lang.String uncommon
class java.lang.String uncommon
class java.lang.String uncommon
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String common
class java.lang.String land
class java.lang.String marketing

答案 2 :(得分:0)

导致异常的原因是助推器:

"booster": [
  [
    "rare",
    "mythic rare"
  ],
...

您在booster中启动了一个数组,但是它不希望有对象,而是需要 string

public class Set
{
    public String name;
    public String code;
    public String releaseDate;
    public String border;
    public String type;
    public List<String> booster; // here it expects list of string
...

将助推器类型从List<String>更改为List<Object>,并反序列化json。