【发布时间】:2013-04-10 11:26:18
【问题描述】:
我想解析来自服务器的 http json 响应的大响应。创建响应文件的类是有害的,因为文件太大。我用gson试过了,但没有效果
这是回复http://www.sendspace.pl/file/ff7257d27380cf5e0c67a33
还有我的代码:
try {
JsonReader reader = new JsonReader(content);
JsonElement json = new JsonParser().parse(reader);
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("devices")) {
System.out.println("gfdd");
} else {
reader.skipValue(); //avoid some unhandle events
}
}
reader.endObject();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我遇到了异常:
Exception in thread "main" java.lang.IllegalStateException: Expected BEGIN_OBJECT but was END_DOCUMENT at line 799 column 2
at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
at LightsControl.main(LightsControl.java:41)
【问题讨论】:
-
JSON 响应确实很大,你想访问响应中的所有数据吗?在 GSON 中,您必须创建类来包装 JSON 响应,我可以向您解释如何,但它确实会“因为文件太大而有害”...