Home > Software engineering >  How can i get precise data in precise json request with java
How can i get precise data in precise json request with java

Time:01-17

i recently started to developp a java discord bot that can use some API of a hypixel skyblock game. 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.

The request with Skycrypt API is something like that : https://sky.shiiyu.moe/api/v2/profile/[Name]. (Where name is your ign on 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".

And when i try to do this in java i'm stuck because i need in fact the d6751... key to do it :

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. 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. 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.

CodePudding user response:

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. 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.

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:

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