使用GSON时应为BEGIN_ARRAY,但应为BEGIN_OBJECT [英] Expected BEGIN_ARRAY but was BEGIN_OBJECT when using GSON

查看:300
本文介绍了使用GSON时应为BEGIN_ARRAY,但应为BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我知道已经问过许多类似的问题,但是我有一个具体的问题,其他所有问题都没有.我想知道如何继续使用GSON解析以下JSON文件.

Ok, I know that many of questions like that have been asked, but I have a specific question, which none of the others has. I want to know how I'd go on to parse following JSON file with GSON.

{
        "BUYER": {
                "IGN": "MGlolenstine",
                "ProductID": "51"
        },
        "BUYER": {
                "IGN": "MGlolenstine",
                "ProductID": "55"
        },
        "BUYER": {
                "IGN": "MGlolenstine",
                "ProductID": "0"
        },
        "BUYER": {
                "IGN": "MGlolenstine",
                "ProductID": "51"
        },
        "BUYER": {
                "IGN": "MGlolenstine",
                "ProductID": "56"
        }
}

因为我使用这段代码

Scanner scanner = new Scanner( new File(path) );
String text = scanner.useDelimiter("\\A").next();
Gson gson = new GsonBuilder().create();
ArrayList<Purchases> p = gson.fromJson(new FileReader(path), Purchases.class);
for(int i = 0; i < p.size(); i++){
    arg0.sendMessage(ChatColor.GOLD+"Player: "+p.get(i).BUYER.IGN);
    arg0.sendMessage(ChatColor.GOLD+"ProductID: "+String.valueOf(p.get(i).BUYER.ProductID));
}
scanner.close();

我得到了错误

Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 2 column 12

这里刚刚发布了我拥有的JSON代码类

Here just posting the classes I have for the JSON code

public class Purchases{
    PlayerRank BUYER;
}

public class PlayerRank{
    String IGN;
    int ProductID;
}

问题可能是我不知道JSON数组和对象的外观. 有人可以在我的JSON代码中解释JSONArray和JSONObject之间的区别吗?

The problem is probably me not knowing how the JSON arrays and objects look like. Could someone please explain the difference between JSONArray and JSONObject in my JSON code?

谢谢.

这是固定的JSON

{
"buyers" : [
    { "IGN" : "MGlolenstine", "ProductID" : "51" },
    { "IGN" : "MGlolenstine", "ProductID" : "55" },
    { "IGN" : "MGlolenstine", "ProductID" : "0" },
    { "IGN" : "MGlolenstine", "ProductID" : "51" },
    { "IGN" : "MGlolenstine", "ProductID" : "56" }
]

}

固定的Java代码:

Scanner scanner = new Scanner( new File(path) );
String text = scanner.useDelimiter("\\A").next();
Gson gson = new GsonBuilder().create();
Purchases p = gson.fromJson(new FileReader(path), Purchases.class);
for(int i = 0; i < p.buyers.length; i++){
    arg0.sendMessage(ChatColor.GOLD+"Player: "+p.buyers[i].IGN);
    arg0.sendMessage(ChatColor.GOLD+"ProductID: "+String.valueOf(p.buyers[i].ProductID));
}

最后是课程:

public class Purchases{
    PlayerRank buyers[];
}

public class PlayerRank{
    String IGN;
    int ProductID;
}

感谢大家的帮助!

推荐答案

JSON对象直接放在大括号{}中,而JSON数组直接放在JSON对象内部的方括号[]中.

JSON Objects are enclosed directly in curly brackets {} vs. JSON Arrays that are enclosed in square brackets [] inside JSON Objects.

Purchases和PlayerRank类应按以下方式定义:

The classes Purchases and PlayerRank should be defined in this way:

public class Purchases{
    @SerializedName("buyers") protected ArrayList<PlayerRank> buyers;

    ...
}

public class PlayerRank{
    @SerializedName("IGN") protected String ign;
    @SerializedName("ProductID") protected int productId;

    ...
}

请注意SerializedName表示法,该表示法使您可以将json文件中的对象/数组的名称与Java属性的名称分离.

Note the SerializedName notation that lets you decouple the name of the objects/arrays in the json file from the names of your java properties.

我添加到属性中的受保护仅使它明确表明了原始类在原始代码中的默认设置.

The protected I added to the properties just makes it explicit what the original classes defaulted to in the original code.

JSON文件应如下所示:

The JSON file should be something like this:

{
        "buyers" : [
            { "IGN": "MGlolenstine", "ProductID": "51"},
            { "IGN": "MGlolenstine", "ProductID": "55"},
            ...
            { "IGN": "MGlolenstine", "ProductID": "56"}
        ]
}

并将JSON读入变量:

And to read the JSON into a variable:

Purchases p = gson.fromJson(new FileReader(path), Purchases.class);

这篇关于使用GSON时应为BEGIN_ARRAY,但应为BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆