解析大型JSON数据时Android中的JsonReader异常

时间:2013-05-21 13:05:20

标签: java android json

我在解析大型json数据时在android中使用JsonReader类时遇到异常。

例外:

java.lang.IllegalStateException: Expected a name but was STRING

json数据有效,但我找不到这个例外的原因!

非常感谢!

我的代码:

    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")&& reader.peek() != JsonToken.NULL) {

             String om_id = reader.nextString();
     }
         else if (name.equals("om_multiselect")&& reader.peek() != JsonToken.NULL) {      

             String om_multiselect = reader.nextString();
     }
         else if (name.equals("omlang_name")&& reader.peek() != JsonToken.NULL) {

             String omlang_name = reader.nextString();
     }
         else if (name.equals("om_createdDate")&& reader.peek() != JsonToken.NULL) {

             String om_createdDate = reader.nextString();
     }
         else
         {
 reader.skipValue();
 }


        }

        reader.endObject();

    }

这是JSON数据:

 {
        "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"
                }
            ]

        }
    }

这是我的示例代码请查看,我成功解析了选项数组,我无法解析optionmaster在尝试解析“om_id”时出现的错误

logcat的:

 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)........

2 个答案:

答案 0 :(得分:0)

我关注短路和检查reader.peek没有执行。 请先阅读reader.peek(),然后再检查ifs

if (reader.peek()!=JsonToken.NULL) {
    if (name.equals("om_id")) {
        String om_id = reader.nextString();
    } else if (name.equals("om_multiselect")) {      
        String om_multiselect = reader.nextString();
    } else if (name.equals("omlang_name")) {
        String omlang_name = reader.nextString();
    } else if (name.equals("om_createdDate")) {
        String om_createdDate = reader.nextString();
    } else {
        reader.skipValue();
    }
}

祝你好运

答案 1 :(得分:-1)

使用此

while(jsonReader.peek()!= JsonToken.END_DOCUMENT)