Search code examples
javajsonminecraftbukkit

Java - Access JSON Element (llegalFormatConversionException: d !=java.lang.String)


I am trying to learn JAVA and build a plugin for Minecraft; I have been successfully able to get the JSON data from my api endpoint however, the issue I am facing right now is an llegalFormatConversionException: d !=java.lang.String which means that the format I am trying to make into string isn't equal to the type of string that it's looking for.

I am trying to access a JSON element from my endpoint called condition

{
    "todaysdate": "2021-02-12",
    "temperature": 25,
    "description": [
        "Overcast"
    ],
    "condition": 122,
}

Coming from C#; I know there's a website called json2sharp where you can create a root class for the JSON. I'm not sure how it would be applied in Java but currently, my code looks like this.

    private String fetchWeather() throws IOException, InvalidConfigurationException {
        // Download
        final URL url = new URL(API);
        final URLConnection request = url.openConnection();

        // Set HEADER
        request.setRequestProperty("x-api-key", plugin.apiKey);


        request.setConnectTimeout(5000);
        request.setReadTimeout(5000);

        request.connect();

        // Convert to a JSON object to print data
        JsonParser jp = new JsonParser(); //from gson
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element

        JsonObject rootobj = root.getAsJsonObject(); 
        //JsonElement code = rootobj.get("condition"); 

        String condition_code = rootobj.get("condition").toString();

        plugin.getLogger().fine(String.format(
                "[%s] Weather is %d",
                world.getName(), condition_code
        ));

        return condition_code;
    }

If I call

    private JsonObject fetchWeather() throws IOException, InvalidConfigurationException {
        // Download
        final URL url = new URL(API);
        final URLConnection request = url.openConnection();

        // Set HEADER
        request.setRequestProperty("x-api-key", plugin.apiKey);


        request.setConnectTimeout(5000);
        request.setReadTimeout(5000);

        request.connect();

        // Convert to a JSON object to print data
        JsonParser jp = new JsonParser(); //from gson
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element

        JsonObject rootobj = root.getAsJsonObject(); 

        return rootobj;
    }
            state = fetchWeather();
            plugin.getLogger().warning(state.toString());

I do get the actual JSON with all of the elements so I know the URL and accessing it is completely working, but I get an llegalexception for format if I try to call and print with logger the condition code.

So, if I change the return type in fetchWeather() to be the root json object and then try to print it with the state variable above it works; but if I try to return the condition json element and print it it gives me an iilegal exception.

Now before I posted this question I did read some other questions people had but I couldn't get a working solution from their suggested answers. So, I am hoping someone can point me out on what I'm doing wrong because I know I'm messing up somewhere with the variable format.

Thanks.

Line 37: state = fetchWeather();
Line 103:
        plugin.getLogger().fine(String.format(
                "[%s] Weather is %d",
                world.getName(), bId
        ));

Solution

  • condition_code variable is String. You should use the %s format specifier.