简体   繁体   English

位置0(JSON)处出现意外令牌'文件结束'

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

I'm trying to retreive a name from a UUID via a website, using the API Mojang provided. 我正在尝试使用提供的API通过网站从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$2.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. 现在,我已经通过一些JSON在线解析器运行返回的json。 They all tell me the JSON is correct. 他们都告诉我JSON是正确的。 This is the JSON that is being returned: 这是返回的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? 那意外的令牌在哪里?

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

In my case I was trying to parse empty file. 就我而言,我试图解析空文件。 So, I just added simple check before parsing and issue had disappeared. 因此,我只是在解析和问题消失之前添加了简单的检查。

暂无
暂无

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

相关问题 将BufferedReader解析为JSON对象时,位置处出现意外的令牌END OF FILE - Unexpected token END OF FILE at position while parsing BufferedReader to JSON Object 解析 JSON 时位置 0 处出现意外标记 END OF FILE - Unexpected token END OF FILE at position 0 while parsing JSON Java Eclipse - 尝试从 JSON 读取时“位置 0 处出现意外令牌 END OF FILE” - Java Eclipse - 'Unexpected token END OF FILE at position 0' when trying to read from JSON 在Java中解析时,JSON值中的空格会导致“位置11处的意外令牌END OF FILE”异常 - A space in JSON value causes “unexpected token END OF FILE at position 11” exception when parsing in Java 错误:语法错误:JSON 中的意外标记 g 在位置 0 - error:SyntaxError: Unexpected token g in JSON at position 0 Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0 - Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0 JSON 中位置 0 处的意外标记 N,用于 Angular 中的 post 方法 - Unexpected token N in JSON at position 0 for post method in angular 来自 Spring 响应的位置 0 处的 JSON 中的意外令牌 S - Unexpected token S in JSON at position 0 from Spring response SyntaxError:带有Servlet的JSON在位置3 Angular2处的意外令牌&lt; - SyntaxError: Unexpected token < in JSON at position 3 Angular2 with Servlet 如何从外部库中捕获“意外令牌结尾文件” - How to catch an “Unexpected token END OF FILE” from an external library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM