简体   繁体   English

解析 JSON 时位置 0 处出现意外标记 END OF FILE

[英]Unexpected token END OF FILE at position 0 while parsing JSON

In order to find out weather the JSON element is JSONArray or JSONObject type, I am getting Unexpected token END OF FILE at position 0 error.为了找出天气 JSON 元素是 JSONArray 或 JSONObject 类型,我在位置 0错误中收到Unexpected token END OF FILE

My JSON is:我的 JSON 是:

{"colors":[{"color":"red","value":"#00"},{"color":"white","value":"#000g"}]}

My code is:我的代码是:

java.io.FileReader reader = new java.io.FileReader("jsonpath");
org.json.simple.parser.JSONParser parser = new JSONParser();
System.Out.Println("aaaaaa JSON Class: "+parser.parse(reader).getClass());
if(parser.parse(reader) instanceof org.json.simple.JSONArray)
    System.Out.Println("JSONArray");
else if(parser.parse(reader) instanceof org.json.simple.JSONObject)
    System.Out.Println("JSONObject");

When I run above code it shows this output当我运行上面的代码时,它显示了这个输出

aaaaaa JSON Class: class org.json.simple.JSONObject Unexpected token END OF FILE at popsition 0
at org.json.simple.parser.JSONParser(Unknown Source)
.
.
.
<rest of the exception>

I don't understand why this exception is occurring.我不明白为什么会发生这种异常。 Please help me out.请帮帮我。

Some more details after edit:编辑后的更多细节:

My this code is working fine with the given json file:我的这段代码在给定的 json 文件中工作正常:

java.io.FileReader reader = new java.io.FileReader("jsonpath");
org.json.simple.parser.JSONParser parser = new JSONParser();
org.json.simple.JSONObject object = (JSONObject)parser.parse(reader);
System.Out.Println("JSONObject: "+object);
org.json.simple.JSONArray array = (JSONArray)object.get("colors");
System.Out.Println("JSONArray: "+array);

Output of above code:上面代码的输出:

JSONObject: {"colors":[{"color":"red","value":"#00"},{"color":"white","value":"#000g"}]}
JSONArray: [{"color":"red","value":"#00"},{"color":"white","value":"#000g"}]

But I want to dynamically parse the JSON without knowing the JSON structure.但是我想在不知道JSON结构的情况下动态解析JSON。 I want to do something like this:我想做这样的事情:

  if(json is object)
    JSONObject object = (JSONObject)parser.parse(reader);
  else if (json is array)
    JSONArray array = (JSONArray)parser.parse(reader);

Thanks.谢谢。

You're repeatedly parsing the same Reader .您反复解析同一个Reader The first call exhausts it and then each subsequent call sees an empty stream.第一个调用耗尽它,然后每个后续调用看到一个空流。

Parse the reader only once.仅解析读卡器一次。 Here is the working code:这是工作代码:

java.io.FileReader reader = new java.io.FileReader("jsonpath");
org.json.simple.parser.JSONParser parser = new JSONParser();
Object p = parser.parse(reader);
if(p instanceof org.json.simple.JSONArray){
    System.Out.Println("JSONArray");
    org.json.simple.JSONArray object = (JSONArray)p;
}
else if(p instanceof org.json.simple.JSONObject){
    System.Out.Println("JSONObject");
    org.json.simple.JSONObject object = (JSONObject)p;
}

Output of above code上面代码的输出

JSONObject

Well, error may occur when you try to pass wrong path.好吧,当您尝试传递错误的路径时可能会发生错误。

So check your path to json file properly.因此,请正确检查 json 文件的路径。 Try to use absolute path at first.首先尝试使用绝对路径。

Here is my procedure:这是我的程序:

private static void ReadWithEncoding(String filePath, String encoding) {
    StringBuilder json = new StringBuilder();
    File f = new File(filePath);
    if (f.exists() && f.isFile()) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), encoding));
            String line;
            while ((line = br.readLine()) != null) {
                json.append(line);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(json);
    }
}

You may run it like this for UTF8:你可以像这样为 UTF8 运行它:

ReadWithEncoding("D:/file.json", "UTF8");

For Cyrillic symbols:对于西里尔符号:

ReadWithEncoding("D:/file.json", "Cp1251");

I too was getting this error ("Unexpected token END OF FILE at position 0.").我也收到此错误(“位置 0 处出现意外令牌 END OF FILE。”)。 I was using same instance of com.google.gson.Gson and org.json.simple.parser.JSONParser on multiple threads.我在多个线程上使用 com.google.gson.Gson 和 org.json.simple.parser.JSONParser 的相同实例。 Now I changed the code and created new instance of these on each thread, and that solved the issue.现在我更改了代码并在每个线程上创建了这些的新实例,这解决了问题。

gson = new GsonBuilder().create(); gson = new GsonBuilder().create(); parser = new JSONParser();解析器 = 新的 JSONParser();

声明:本站的技术帖子网页,遵循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 位置0(JSON)处出现意外令牌&#39;文件结束&#39; - Unexpected token 'END OF FILE' at position 0 (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 Java Eclipse - 尝试从 JSON 读取时“位置 0 处出现意外令牌 END OF FILE” - Java Eclipse - 'Unexpected token END OF FILE at position 0' when trying to read from JSON System.Xml.XmlException:解析时文件意外结束 - System.Xml.XmlException: Unexpected end of file while parsing 错误:语法错误: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 解析时到达文件末尾 - reached end of the file while parsing 解析时到达文件末尾? - reached end of file while parsing? 使用非常规令牌解析json文件 - Parsing json file with an unconventional token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM