0

I thought this would be simple but it keeps crashing with InvocationTargetException at reader.hasNext().

    File jsonInputFile = new File(root + "/dicts/phrases.json");
    JsonReader reader = new JsonReader(new FileReader(jsonInputFile));
    while (reader.hasNext()) {
        System.out.println(reader.nextName());
    }

What exactly is wrong with this? The while loop just goes on forever despite the json file only have one line.

Hasen
  • 11,710
  • 23
  • 77
  • 135
  • 1
    ....can you close a reader before you do reader.hasNext() ?? – JCompetence Oct 22 '21 at 11:05
  • @Susan Mustafa Removed the `reader.close();` but it doesn't change anything. I still get the same `InvocationTargetException` error on the same line. The while loop is infinite and any call to `reader.nextName()` throws the error. – Hasen Oct 22 '21 at 11:32
  • Could you kindly paste a portion of your json? Have you tried with a simple json...just 1 element in it to see if it is the data that could potentially cause you problems? – JCompetence Oct 22 '21 at 11:52
  • [detailed information about the error](https://stackoverflow.com/a/6020754/12348326) So can you share the details of the error? Maybe it's easier to understand the cause of the problem. – BroscR Oct 22 '21 at 11:54
  • @BroscR `Caused by: java.lang.IllegalStateException: Expected a name but was @BEGIN_OBJECT at line 1 column 2 path $ at com.google.gson@2.8.8/com.google.gson.stream.JsonReader.nextName(JsonReader.java:788)` – Hasen Oct 22 '21 at 11:59
  • @BroscR It seems to me what is happening is it's just not reading the file in the first two lines, that's why the while loop is infinite...? But I've no idea why since I've confirmed the filename is correct. – Hasen Oct 22 '21 at 12:02

1 Answers1

2

Since it seems to be reading the JSON and it is encountering a BEGIN_TOKEN rather than JsonToken.NAME type

Could you try the below:

           while (jsonReader.hasNext()) 
            {
                JsonToken nextToken = jsonReader.peek();
                 
                if (JsonToken.BEGIN_OBJECT.equals(nextToken)) {
 
                    jsonReader.beginObject();
 
                } else if (JsonToken.NAME.equals(nextToken)) {
 
                    String name = jsonReader.nextName();
                    System.out.println("Token KEY >>>> " + name);
 
                } else if (JsonToken.STRING.equals(nextToken)) {
 
                    String value = jsonReader.nextString();
                    System.out.println("Token Value >>>> " + value);
 
                } else if (JsonToken.NUMBER.equals(nextToken)) {
 
                    long value = jsonReader.nextLong();
                    System.out.println("Token Value >>>> " + value);
 
                } else if (JsonToken.NULL.equals(nextToken)) {
 
                    jsonReader.nextNull();
                    System.out.println("Token Value >>>> null");
                     
                } else if (JsonToken.END_OBJECT.equals(nextToken)) {
 
                    jsonReader.endObject();
 
                }
            }
JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • 1
    Aha! Yes that works. So it was me not reading it correctly that was the problem. – Hasen Oct 22 '21 at 12:12
  • Yes I have it all working perfectly now, it reads the json file in the order it is in the file solving a shortcoming of the `JSONObject` system. Thanks for your help. – Hasen Oct 22 '21 at 12:20