基本上,我有一个链接to a API,它基本上只显示一个巨大的stats JSON文件,如何使用Java最简单地获取在player.stats.Bedwars.winstreak下找到的“ winstreak”的值?
输入:Link to API输出:6
使用http java客户端调用您的API,并将其接受为String(JSON)。然后,您可以使用GSON
库将该String / JSON转换为Java对象,该库用于将JSON解析为Java Object或将Java Object转换为JSON。
用法:
String json = `Your API call here`;
Gson gson = new Gson();
MyClass myObject = gson.fromJson(json, MyClass.class);
You will have to create some Model classes which will follow the same structure as your Json.
For Example:
If your JSON looks like - {"name":"myname","age":8}
Then your MyClass will look like:
Class MyClass{
private String name;
private int age;
//setters & getters
}
If your JSON looks like -
{
"player": {
"stats": {
"Bedwars": {
"winstreak": "your value";
}
}
}
}
Your classes will look like this:
Class MyClass {
@JsonProperty("player")
private Player player;
//setters & getters
}
Class Player {
@JsonProperty("stats")
private Stats stats;
//setters & getters
}
Class Stats {
@JsonProperty("Bedwars")
private Bedwars bedwars;
//setters & getters
}
Class Bedwars {
@JsonProperty("winstreak")
private String winstreak;
//setters & getters
}
Hope this helps.