Search code examples
javajsonjavax.json

trouble with parse data from JSON format


I have a data in JSON format. For example:

{
    "status": "ok",
    "meta": {
        "count": 1
    },
    "data": {
        "531446": {
            "global_rating": 6595,
            "statistics": {
                "random": {
                    "spotted": 14379,
                    "xp": 9087316,
                    "draws": 238,
                    "battles": 15034,
                    "wins": 7614,
                    "avg_damage_assisted_track": 42.94
                }
            }
        }
    }
}

For parsing I have wrote next code. But it doesn't work.

    JsonReader jsonReader = Json.createReader(new FileInputStream("F:\\Test.json"));
    JsonObject jsonObject = jsonReader.readObject();
    jsonReader.close();

    int acc_id = 531446;

    JsonObject dataJson = jsonObject.getJsonObject("data");
    System.out.println("data:" + dataJson);
    JsonObject accountIdJson = dataJson.getJsonObject(String.valueOf(acc_id));

    System.out.println("    accountIdJson:"+accountIdJson);
    String globalRatingJson = accountIdJson.getString("global_rating");

I'm trying to get value of global_rating, and battles, but this code throws exception

    Exception in thread "main" java.lang.ClassCastException:
    org.glassfish.json.JsonNumberImpl$JsonIntNumber cannot be cast to
    javax.json.JsonString

on last line, but i don't understand why. What did i do wrong? Can someone hint me?

thx.


Solution

  • global_rating looks to be an int, no? Try this:

    int global_rating = accountIdJson.getInt("global_rating");