简体   繁体   English

我如何通过 java 在精确的 json 请求中获取精确数据

[英]How can i get precise data in precise json request with java

i recently started to developp a java discord bot that can use some API of a hypixel skyblock game.我最近开始开发一个 java discord 机器人,它可以使用一些 API 的 hypixel skyblock 游戏。 I've done many request so far, but now i'm stuck on one.到目前为止,我已经完成了很多请求,但现在我只剩下一个了。 I want to get the current profile of a player and for that is use Skycrypt API.我想获取玩家的当前个人资料,为此使用 Skycrypt API。

The request with Skycrypt API is something like that: https://sky.shiiyu.moe/api/v2/profile/[Name] . Skycrypt API 的请求是这样的: https://sky.shiiyu.moe/api/v2/profile/[Name] (Where name is your ign on hypixel skyblock) (名称是你在 hypixel skyblock 上的标志)

And the API return something like that: API response On each profile (random number like d675....) i get few fields that are important: Fields The key that i want to get here is "current". API 返回类似这样的内容: API 响应在每个配置文件上(像 d675 这样的随机数......)我得到几个重要的字段:字段我想在此处获取的键是“当前”。

And when i try to do this in java i'm stuck because i need in fact the d6751... key to do it:当我尝试在 java 中执行此操作时,我被卡住了,因为我实际上需要 d6751... 键来执行此操作:

URL pseudo = new URL("https://sky.shiiyu.moe/api/v2/profile/CoopCarried");
BufferedReader in2 = new BufferedReader(new InputStreamReader(pseudo.openStream()));
StringBuilder response2 = new StringBuilder();
String inputLine2;
while ((inputLine2 = in2.readLine()) != null) {
     response2.append(inputLine2);
}in2.close();
JSONObject json = new JSONObject(response2.toString());
Boolean test = json.getJSONObject("profiles").getJSONObject("d675...").getBoolean("current");
System.out.println(test);

And the problem here is that the player can have multiple profiles which means multiple d6751 keys and if i need the keys in question in my code acces the current key, there will be a problem.这里的问题是播放器可以有多个配置文件,这意味着多个 d6751 密钥,如果我需要我的代码中有问题的密钥访问当前密钥,就会出现问题。 And keep it mind that i'm doing it for a guild and i got almost 100 peoples to deal with, so if anyone know a solution that's would be a big help for me.请记住,我正在为一个公会做这件事,我有将近 100 个人要处理,所以如果有人知道解决方案,那将对我有很大帮助。 I'm here if you got some questions that could help you (by the way you can try the API request with my name on minecraft which is: CoopCarried), thank you in advance.如果您有一些可以帮助您的问题,我会在这里(顺便说一句,您可以在 minecraft 上使用我的名字尝试 API 请求,即:CoopCarried),在此先感谢您。

Essentially, what you want to do is loop over the keys in your "profiles" JSON object. An easy way to do this is by using the keySet() method on the JSONObject .本质上,您想要做的是遍历“配置文件”JSON object 中的键。一种简单的方法是使用JSONObject上的keySet()方法。 I assumed that you were using org.json:json because I couldn't see your imports in your code snippet, but if your JSONObject class does not have the keySet() method, there is probably a similar method you could use instead.我假设您使用的是org.json:json ,因为我在您的代码片段中看不到您的导入,但是如果您的JSONObject class 没有keySet()方法,您可能可以使用类似的方法。

Code代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.json.JSONObject;

public class MinecraftThing {
    public static void main(String[] args) throws IOException {
        URL pseudo = new URL("https://sky.shiiyu.moe/api/v2/profile/CoopCarried");
        BufferedReader in2 = new BufferedReader(new InputStreamReader(pseudo.openStream()));
        StringBuilder response2 = new StringBuilder();
        String inputLine2;
        while ((inputLine2 = in2.readLine()) != null) {
            response2.append(inputLine2);
        }
        in2.close();
        JSONObject json = new JSONObject(response2.toString());
        for (final Object keyObject : json.getJSONObject("profiles").keySet()) {
            final String key = keyObject.toString();
            Boolean test = json.getJSONObject("profiles").getJSONObject(key).getBoolean("current");
            System.out.println(key + ": " + test);
        }
    }
}

Output: Output:

dfdb...: false
27d0...: false
d675...: false
2862...: false
6bb9...: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM