Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token END OF FILE at position 0 while parsing JSON

Tags:

java

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.

My JSON is:

{"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:

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:

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

Thanks.

like image 775
kumarhimanshu449 Avatar asked Dec 15 '15 09:12

kumarhimanshu449


3 Answers

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

like image 113
Joe Avatar answered Oct 01 '22 03:10

Joe


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
like image 37
kumarhimanshu449 Avatar answered Oct 01 '22 03:10

kumarhimanshu449


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.

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:

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

For Cyrillic symbols:

ReadWithEncoding("D:/file.json", "Cp1251");
like image 31
Yuriy Kiselev Avatar answered Oct 01 '22 02:10

Yuriy Kiselev