Note that there are some explanatory texts on larger screens.

plurals
  1. POJsonReader Exception in Android while parsing large JSON data
    primarykey
    data
    text
    <p>I am getting an exception when using the <code>JsonReader</code> class in android while parsing a large json data.</p> <p>Exception:</p> <pre><code>java.lang.IllegalStateException: Expected a name but was STRING </code></pre> <p>The json data is valid but I can't find out the reason for this exception!</p> <p>Thanks a lot!</p> <p>My Code :</p> <pre><code> public void readJsonStream(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in)); reader.setLenient(true); try { readJsonObject(reader); } catch (Exception e) { System.out.println(e); } finally { reader.close(); } } // Read Json From JsonReader Class public void readJsonObject(JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); System.out.println(name.equals("result")); if (name.equals("result")) { reader.beginObject(); while (reader.hasNext()) { String josnParser = reader.nextName(); if (josnParser.equals(optionmaster)) { // Optionmaster reader.beginArray(); while (reader.hasNext()) { readJsonArray(reader); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); } else { reader.skipValue(); } } reader.endObject(); } public void readJsonArray(JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); System.out.println(name); // Here is My Exception .... if (name.equals("om_id")&amp;&amp; reader.peek() != JsonToken.NULL) { String om_id = reader.nextString(); } else if (name.equals("om_multiselect")&amp;&amp; reader.peek() != JsonToken.NULL) { String om_multiselect = reader.nextString(); } else if (name.equals("omlang_name")&amp;&amp; reader.peek() != JsonToken.NULL) { String omlang_name = reader.nextString(); } else if (name.equals("om_createdDate")&amp;&amp; reader.peek() != JsonToken.NULL) { String om_createdDate = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); } </code></pre> <p>And here's the JSON data:</p> <pre><code> { "result": { "options": [ { "opt_id": "8", "opt_om_id": "3", "optlang_name": "test1" }, { "opt_id": "9", "opt_om_id": "3", "optlang_name": "test" } ], "optionmaster": [ { "om_id": "2", "om_multiselect": "N", "omlang_name": "Style", "om_createdDate": "2012-08-25 01:37:57" }, { "om_id": "3", "om_multiselect": "Y", "omlang_name": "TEst", "om_createdDate": "2012-08-25 01:37:57" } ] } } </code></pre> <p>Here is my sample code please review , i parsed options array successfully, the i couldn't parse optionmaster the error which appears when trying to parse "om_id"</p> <p>Logcat:</p> <pre><code> 05-21 17:39:54.833: W/System.err(1116): java.lang.IllegalStateException: Expected a name but was STRING 05-21 17:39:54.833: W/System.err(1116): at android.util.JsonReader.nextName(JsonReader.java:390) 05-21 17:39:54.843: W/System.err(1116): at com.test.JsonParser.readJsonArray(JsonParser.java:174) 05-21 17:39:54.843: W/System.err(1116): at com.test.JsonParser.readJsonObject(JsonParser.java:135) 05-21 17:39:54.843: W/System.err(1116): at com.test.JsonParser.readJsonStream(JsonParser.java:49) 05-21 17:39:54.843: W/System.err(1116): at com.test.ItemListActivity$ApiSyncController.doInBackground(ItemListActivity.java:149) 05-21 17:39:54.853: W/System.err(1116): at com.test.ItemListActivity$ApiSyncController.doInBackground(ItemListActivity.java:1) 05-21 17:39:54.853: W/System.err(1116): at android.os.AsyncTask$2.call(AsyncTask.java:287) 05-21 17:39:54.853: W/System.err(1116): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 05-21 17:39:54.863: W/System.err(1116): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 05-21 17:39:54.863: W/System.err(1116): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 05-21 17:39:54.863: W/System.err(1116): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 05-21 17:39:54.863: W/System.err(1116): at java.lang.Thread.run(Thread.java:856)........ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload