从 JSON 文件中获取一个整数

Get an int from a JSON File

我在 JSON 文件解析时遇到问题。我必须从 JSON 文件中获取一个整数(在本例中是玩家 Speedys 的 factionId)。我已经使用了这段代码,但它不起作用..那有什么问题? http://pastebin.com/7gvhHpSz

public static JsonObject convertFileToJSON (String fileName){

    // Read from File to String
    JsonObject jsonObject = new JsonObject();

    try {
        JsonParser parser = new JsonParser();
        JsonElement jsonElement = parser.parse(new FileReader(fileName));
        jsonObject = jsonElement.getAsJsonObject();
    } catch (FileNotFoundException e) {

    } 


    return jsonObject;
}



public static String getJsonString(JsonObject json, String path) {
    String[] parts = path.split("\.");
    JsonObject next = null;
    if (parts.length == 1) {
        return json.get(parts[0]).getAsString();
    }
    for (int i = 0; i < parts.length; i++) {
        if (next == null) {
            next = json.getAsJsonObject(parts[i]);
            continue;
        }
        if (i == (parts.length - 1)) {
            return next.get(parts[i]).getAsString();
        }
        else {
            next = next.getAsJsonObject(parts[i]);
        }
    }
    return null;


}

public boolean onCommand(CommandSender sender, Command cmd, String label,
        String[] args) {
    Player player = (Player) sender;
    if (cmd.getName().equalsIgnoreCase("myfp")) {

        if (args.length == 0) {
            //do something
        } else if (args[0].equalsIgnoreCase("pacifica")) {

            JsonObject object = convertFileToJSON ("plugins/Factions/players.json");
            int number = getJsonString(object, "Speedys.factionId");
            Bukkit.broadcastMessage(text);

        }

    }
    return false;
}

我正在使用 GSON 和 Bukkit。

{
  "Speedys": {
    "factionId": "148",
    "role": "ADMIN",
    "title": "",
    "power": 8.948716666666668,
    "powerBoost": 0.0,
    "lastPowerUpdateTime": 1425205197880,
    "lastLoginTime": 1425205197880,
    "chatMode": "PUBLIC"
  },
  "stobbie511": {
    "factionId": "0",
    "role": "NORMAL",
    "title": "",
    "power": 0.7870366666666667,
    "powerBoost": 0.0,
    "lastPowerUpdateTime": 1425233475349,
    "lastLoginTime": 1425233475349,
    "chatMode": "PUBLIC"
  },
  "demonofloom": {
    "factionId": "265",
    "role": "NORMAL",
    "title": "",
    "power": -10.0,
    "powerBoost": 0.0,
    "lastPowerUpdateTime": 1425304265606,
    "lastLoginTime": 1425300330188,
    "chatMode": "PUBLIC"
  }
}

结果应该是“148”。

打印出来的错误是:

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to java.lang.String

小例子,使用 GSON 解析:

Gson gson = new Gson();
Speedys speedy = gson.fromJson(String.valueOf("YOUR JSON OBJECT"), Speedys.class);
int n = speedy.getlastLoginTime();

还有 Speedys class:

public class Speedys {

    @SerializedName("factionId")
    private String factionId;

    @SerializedName("lastLoginTime")

    private int lastLoginTime;


      public String getFactionId() {
        return factionId;
    }

    public void setFactionId(String factionId) {
        this.factionId = factionId;
    }
public int getlastLoginTime() {
        return lastLoginTime;
    }

    public void setlastLoginTime(int lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }