JsonReader
在解析大型 json 数据时使用 android 中的类时出现异常。
例外:
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”时出现的错误
日志猫:
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)........