Expected BEGIN_ARRAY but was BEGIN_OBJECT when using GSON

故事扮演 提交于 2020-05-15 21:25:37

问题


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

because when I use this code

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();

I get the error

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

Here just posting the classes I have for the JSON code

public class Purchases{
    PlayerRank BUYER;
}

public class PlayerRank{
    String IGN;
    int ProductID;
}

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?

Thank you in advance.

EDIT: So this is the fixed JSON

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

}

Fixed Java code:

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

And lastly the classes:

public class Purchases{
    PlayerRank buyers[];
}

public class PlayerRank{
    String IGN;
    int ProductID;
}

Thanks to everyone for the help!


回答1:


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

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;

    ...
}

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.

The JSON file should be something like this:

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

And to read the JSON into a variable:

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


来源:https://stackoverflow.com/questions/43821497/expected-begin-array-but-was-begin-object-when-using-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!