2

I have a json file of about 30mb jsonFileInputStream.json.

 [ 
        ["aaa", "kkk", "1674"],
        ["bbb", "jjj", "1673"],
        ["ccc", "qqq", "1677"],

                  ..... // very long stream of data

        ["ddd", "zzz", "1677"]]

This is very long stream which I can not store as object. I have seen following question, but it stores as object, which I can not do.

"Java: parse Array of Arrays from JSON"

While loop gives error as:

Caused by: java.lang.IllegalStateException: Expected END_ARRAY but was BEGIN_ARRAY at line 1 column....

I have following code, and I am getting error if first string does not match in any array.

try {
            JsonReader reader = new JsonReader(new InputStreamReader(jsonFileInputStream, "UTF-8"));
            Gson gson = gsonBuilder.create();
            reader.beginArray();

            reader.beginArray();
            while (reader.hasNext()) {
                String abc = reader.nextString();
               // check first string with input string
               // it checks for matching string in json stream.
                if (barCode.equals(abc) ) {
                    String name2 = reader.nextString();
                    String name3 = reader.nextString();



                    boolean found = true;
                    break;
                } else {

                    reader.nextString();
                    reader.nextString();

                    reader.endArray();
                    reader.beginArray();

                }
            }
            reader.endArray();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

My error stack trace looks like:

java.lang.IllegalStateException: Expected END_ARRAY but was BEGIN_ARRAY at line 1 column 218 path $[1]

                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
                  at android.os.Handler.dispatchMessage(Handler.java:110)
                  at android.os.Looper.loop(Looper.java:203)
                  at android.app.ActivityThread.main(ActivityThread.java:6251)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                   Caused by: java.lang.IllegalStateException: Expected END_ARRAY but was BEGIN_ARRAY at line 1 column 218 path $[1]
                  at com.google.gson.stream.JsonReader.endArray(JsonReader.java:368)
                  at com.android.features.home.TestActivity.readBarcodeFromMainDb(TestActivity)
oneDumb
  • 21
  • 2

1 Answers1

0

I think the problem occurs when the condition

barCode.equals(aaa)

is never satisfied. When you go in the else branch what happens is the following:

["bbb", "jjj", "1673"],
["ccc", "qqq", "1677"],

you read it like:

...
reader.endArray();
reader.beginArray();

So after an "]" you call reader.endArray() and right after that you call reader.beginArray() because you are expecting the next array.

If you go in the else statement at the end of the file you will end up at the end of your json file:

["ddd", "zzz", "1677"]]

Right after the first "]" you find another "]" and the stack trace is telling you exactly that:

Expected END_ARRAY but was BEGIN_ARRAY at line 1 column

So you need to make sure that you handle the situation where the condition on your if statement is never true.

Also, you are clearly reading

String abc = reader.nextString();

But you are NOT using the abc variable in the if statement. I am not sure this is what you want. What exactly is the "barCode" variable here?

Anyhow, to fix the problem just change the code as follows:

        ...
        Gson gson = gsonBuilder.create();
        reader.beginArray();

        while (reader.hasNext()) {
            reader.beginArray();  //Moved this statement from outside the while to here
            String abc = reader.nextString();
            if (abc.equals(aaa) ) {
                String name2 = reader.nextString();
                String name3 = reader.nextString();



                boolean found = true;
                break;
            } else {

                reader.nextString();
                reader.nextString();

                reader.endArray();
                //Remove the reader.beginArray() from here


            }
        }
arocketman
  • 1,134
  • 12
  • 21