Java 位置 0 处的意外标记“END OF FILE”(JSON)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24636454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:36:52  来源:igfitidea点击:

Unexpected token 'END OF FILE' at position 0 (JSON)

javajsonparsingtokeneof

提问by Staartvin

I'm trying to retreive a name from a UUID via a website, using the API Mojang provided.

我正在尝试使用提供的 API Mojang 通过网站从 UUID 中检索名称。

This is the code I use (I verified that it works):

这是我使用的代码(我验证它有效):

@Override
public Map<UUID, String> call() throws Exception {
    Map<UUID, String> uuidStringMap = new HashMap<UUID, String>();
    for (UUID uuid : uuids) {
        HttpURLConnection connection = (HttpURLConnection) new URL(
                PROFILE_URL + uuid.toString().replace("-", ""))
                .openConnection();

        JSONObject response;

        System.out.println("String: " + fromStream(connection.getInputStream()));
        System.out.print("Line: " + uuid.toString());

        String name = null;

        try {
            response = (JSONObject) jsonParser
                    .parse(new InputStreamReader(connection.getInputStream()));

            name = (String) response.get("name");
            if (name == null) {
                continue;
            }
            String cause = (String) response.get("cause");
            String errorMessage = (String) response.get("errorMessage");
            if (cause != null && cause.length() > 0) {
                throw new IllegalStateException(errorMessage);
            }
        } catch (Exception e) {
            System.out.print("Could not parse uuid '" + uuid.toString() + "' to name!");
            System.out.print("Trying Bukkit.getOfflinePlayer()");

            OfflinePlayer oPlayer = Bukkit.getOfflinePlayer(uuid);

            if (oPlayer != null && oPlayer.getFirstPlayed() != 0) {
                name = oPlayer.getName();

                System.out.print("Got player " + name);
            } else {
                System.out.print("Could not retrieve player with Bukkit.getOfflinePlayer()");
                e.printStackTrace();
            }
            //throw new Exception("Could not parse uuid '" + uuid.toString() + "' to name!");
        }


        uuidStringMap.put(uuid, name);
    }
    return uuidStringMap;
}

Don't mind all the System.out.print lines, that's just to debug things.

不要介意所有 System.out.print 行,这只是为了调试。

It errors with this error:

它出现此错误:

[17:35:56 WARN]: Unexpected token END OF FILE at position 0.
[17:35:56 WARN]:        at org.json.simple.parser.JSONParser.parse(Unknown Source)
[17:35:56 WARN]:        at org.json.simple.parser.JSONParser.parse(Unknown Source)
[17:35:56 WARN]:        at me.armar.plugins.autorank.util.uuid.NameFetcher.call(
NameFetcher.java:59)
[17:35:56 WARN]:        at me.armar.plugins.autorank.util.uuid.UUIDManager.run
(UUIDManager.java:222)
[17:35:56 WARN]:        at java.lang.Thread.run(Unknown Source)

Now, I've run the returned json through a few JSON online parsers. They all tell me the JSON is correct. This is the JSON that is being returned:

现在,我已经通过一些 JSON 在线解析器运行了返回的 json。他们都告诉我 JSON 是正确的。这是正在返回的 JSON:

{"id":"cb994e15a57a4157953ab29577229ebe","name":"tator0 1","properties":[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0MDQ4MzM3NTM0MTYsInByb2ZpbGVJZCI6ImNiOTk0ZTE1YTU3YTQxNTc5NTNhYjI5NTc3MjI5ZWJlIiwicHJvZmlsZU5hbWUiOiJ0YXRvcjAxIiwiaXNQdWJsaWMiOnRydWUsInRleHR1cmVzIjp7IlNLSU4iOnsidXJsIjoiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS80Nzk5MjQ4NGJmNjUyODJhMTQzOWI0MGQ1NWU2ZTRjZGQxNGRmYzAyNGE0MzA3Y2M2YTM1M2Q4MzY1OThkZDUifX19","signature":"t93OEZ3BNa4vo47diKixJA0KWXqblUtWC9yZtTImuxfbSs0khqV02p58eeDF4Qbd4gy6VBQ0zv6N9oxRqhTKBQ1BG1FJHTfhGXFlLYlhHlXmVC5FMBnK0P//0T0PqVz7hlFgzW3YK6vaEf3tz5v5H/S+aCjZbCZ9vehDRx8vrV+SzZek0vuSHjm9ojooqwD4WQjCg1ZPZPtpYD/efj1OcTmS6xEnBEXbCTqOnK/wRbwfyKtezkJutFJ4UCrVCLos85ze79mMfBr67CtotFcQrVqRE9X5zShLatCCVlefPRo6K7TcKKzQYlTM24asaySfZYpv0ujGeHX2c6s4nKvg6vnwLnHT2RwDuJucCojAeoy9pRFq8jEtPSoiPGlA37NlLjChuLXp3cSYGhKIpgDFcXXNF+YsYOlN2XuYa2OpAt8EgDj09gd8r903a2apIVUDo4Hmln9rlI9J74KdifEZc3uUyazlP2BByID2YBcKr5mq5ye+MOKWkN8j5AaPNUKPNpFBXHkOOF/uE72VoBpsjDm0X9OfEx7xtNu+bA4jBObxnQKsD2qIvyhCtlmuN4S6G+VKtsBhiqlO7JTycAn1yij3aw3XhT763gALQNcUkjzERnyp55s8J7FlIpSvObNvndOgypPu4RpsHtkL/G16a/mxRJhljiAasiKX1WsYVig="}],"legacy":true}

I saw that there also was someone else with a kind of similiar issue, here.

我看到,也有带着一种similiar问题,别人在这里

Where could that unexpected token be?

那个意想不到的令牌会在哪里?

回答by LiveMynd

Try applying .toString() to your input string before parsing.

在解析之前尝试将 .toString() 应用于您的输入字符串。

For example:

例如:

DataInputStream in = new DataInputStream(clientSocket.getInputStream());
String dataIn = in.readUTF();
JSONObject jobj = (JSONObject) parser.parse(dataIn.toString());

Worked for me just moments ago. :D

不久前为我工作。:D

回答by Jimilian

In my case I was trying to parse empty file. So, I just added simple check before parsing and issue had disappeared.

就我而言,我试图解析空文件。所以,我只是在解析和问题消失之前添加了简单的检查。