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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34285649/
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-11 15:30:09  来源:igfitidea点击:

Unexpected token END OF FILE at position 0 while parsing JSON

javajson

提问by kumarhimanshu449

In order to find out weather the JSON element is JSONArray or JSONObject type, I am getting Unexpected token END OF FILE at position 0error.

为了找出天气 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. I want to do something like this:

但是我想在不知道JSON结构的情况下动态解析JSON。我想做这样的事情:

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

Thanks.

谢谢。

采纳答案by Joe

You're repeatedly parsing the same Reader. The first call exhausts it and then each subsequent call sees an empty stream.

您反复解析相同的Reader. 第一个调用耗尽它,然后每个后续调用看到一个空流。

回答by kumarhimanshu449

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

回答by Yuriy Kiselev

Well, error may occur when you try to pass wrong path.

好吧,当您尝试传递错误的路径时可能会发生错误。

So check your path to json file properly. Try to use absolute path at first.

因此,请正确检查 json 文件的路径。首先尝试使用绝对路径。

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