0

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!

2
  • Can you post the Puchases class. Besides of what I posted about the JSON format, the class you pass to gson has to mirror the file. Commented May 6, 2017 at 14:43
  • I added the classes part in the question. Thanks for the response Commented May 6, 2017 at 14:51

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you... Will try and will keep you posted!
Added JSON. Can you please verify it? So I'll know that I understood you correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.