簡體   English   中英

Java - 訪問 JSON 元素(llegalFormatConversionException:d.=java.lang.String)

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

我正在嘗試學習 JAVA 並為 Minecraft 構建一個插件; 我已經成功地從我的 api 端點獲得了 JSON 數據但是,我現在面臨的問題是 llegalFormatConversionException: d.=java.lang.String 這意味着我試圖將其轉換為字符串的格式不是等於它正在尋找的字符串類型。

我正在嘗試從名為條件的端點訪問 JSON 元素

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

來自 C#; 我知道有一個名為 json2sharp 的網站,您可以在其中為 JSON 創建一個根 class。 我不確定它將如何應用於 Java 但目前,我的代碼看起來像這樣。

    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;
    }

如果我打電話

    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());

我確實得到了帶有所有元素的實際 JSON,所以我知道 URL 並且訪問它是完全有效的,但是如果我嘗試使用記錄器調用和打印條件代碼,我會收到格式錯誤。

因此,如果我將 fetchWeather() 中的返回類型更改為根 json object 然后嘗試使用上面的 state 變量打印它就可以了; 但是如果我嘗試返回條件 json 元素並打印它,它會給我一個非法異常。

現在,在我發布這個問題之前,我確實閱讀了人們提出的其他一些問題,但我無法從他們建議的答案中獲得有效的解決方案。 所以,我希望有人能指出我做錯了什么,因為我知道我在變量格式的某個地方搞砸了。

謝謝。

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

condition_code 變量是字符串。 您應該使用 %s 格式說明符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM