我有一个类似的JSON:
{“aData”:
{ “锦标赛”:
{ “新”:[
{“token”:“1-token”,“prio”:“6”,“status”:“inactive”,...,“_ timeCreated”:“2014-04-14 14:44:49” ,“_ timeChanged”:“2014-08-08 12:30:20”},
{ “标记”: “2令牌”, “PRIO”: “8”, “状态”: “无效”,...}
]}
}
}
JSON文件大小为8 MB且有效,因为它可以使用默认new JSONObject(str)
或Volley's JsonObjectRequest
进行解析。
我使用的代码:
reader = new JsonReader( new InputStreamReader( am.open( "updatedata.json" ) ) );
reader.setLenient( true );
while( reader.hasNext() ){
eachToken( reader );
Log.i( TAG, "has next " + reader.hasNext() );
}
private boolean eachToken( JsonReader reader ) throws IOException {
JsonToken token = reader.peek();
switch( token ){
case BEGIN_ARRAY:
reader.beginArray();
Log.i( TAG, "array <<" );
break;
case END_ARRAY:
reader.endArray();
Log.i( TAG, "array >>" );
break;
case BEGIN_OBJECT:
reader.beginObject();
Log.i( TAG, "{" );
break;
case END_OBJECT:
reader.endObject();
Log.i( TAG, "}" );
break;
case NAME:
String name = reader.nextName();
Log.i( TAG, name );
break;
case STRING:
String s = reader.nextString();
Log.i( TAG, s );
break;
case NUMBER:
int n = reader.nextInt();
Log.i( TAG, "" + n );
break;
case BOOLEAN:
boolean b = reader.nextBoolean();
Log.i( TAG, "" + b );
break;
case NULL:
reader.nextNull();
Log.i( TAG, "null" );
break;
case END_DOCUMENT:
Log.i( TAG, "end doc" );
return false;
default:
Log.i( TAG, token.toString() );
}
return true;
}
问题是,永远不会达到案例END_OBJECT
。它将以下内容打印到logcat
:
01-06 13:30:32.817: I/DataService(1819): {
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.817: I/DataService(1819): aData
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.817: I/DataService(1819): {
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.817: I/DataService(1819): tournaments
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.817: I/DataService(1819): {
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.817: I/DataService(1819): new
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.817: I/DataService(1819): array <<
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.817: I/DataService(1819): {
01-06 13:30:32.817: I/DataService(1819): has next true
01-06 13:30:32.827: I/DataService(1819): token
01-06 13:30:32.827: I/DataService(1819): has next true
01-06 13:30:32.827: I/DataService(1819): 1-token
01-06 13:30:32.827: I/DataService(1819): has next true
01-06 13:30:32.827: I/DataService(1819): prio
01-06 13:30:32.827: I/DataService(1819): has next true
01-06 13:30:32.827: I/DataService(1819): 6
01-06 13:30:32.827: I/DataService(1819): has next true
01-06 13:30:32.827: I/DataService(1819): status
01-06 13:30:32.827: I/DataService(1819): has next true
01-06 13:30:32.827: I/DataService(1819): inactive
01-06 13:30:32.837: I/DataService(1819): has next true
01-06 13:30:32.837: I/DataService(1819): _timeCreated
01-06 13:30:32.837: I/DataService(1819): has next true
01-06 13:30:32.837: I/DataService(1819): 2014-04-14 14:44:49
01-06 13:30:32.837: I/DataService(1819): has next true
01-06 13:30:32.837: I/DataService(1819): _timeChanged
01-06 13:30:32.837: I/DataService(1819): has next true
01-06 13:30:32.837: I/DataService(1819): 2014-08-08 12:30:20
01-06 13:30:32.837: I/DataService(1819): has next false
因此,它到达第一个对象的"_timeChanged":"2014-08-08 12:30:20"
,然后reader.hasNext()
转为false
,而不是移动到新的BEGIN_OBJECT
令牌。
答案 0 :(得分:2)
hasNext()
说“如果当前数组或对象有另一个元素,则返回true。”在您的情况下,当它到达对象或数组hasNext()
的末尾时false
,case END_ARRAY
和case END_OBJECT
不会被调用。
这可能有效:
while( reader.hasNext()
|| reader.peek() == END_ARRAY
|| reader.peek() == END_OBJECT ){
eachToken( reader );
Log.i( TAG, "has next " + reader.hasNext() );
}
这与Joey的答案基本相同,但不适合评论。
答案 1 :(得分:1)
我想问题可能在于您处理数据的方式
如果查看JsonReader
的{{3}},它会特别定义您需要使用数组括号..
在数组处理方法中,首先调用beginArray()来使用数组的左括号。然后创建一个累积值的while循环,当hasNext()为false时终止。最后,通过调用endArray()来读取数组的结束括号。
数组完成后hasNext()
为false ...