我有一个像这样的 JSON:
{“数据”:
{“比赛”:
{“新的”:[
{"token":"1-token", "prio":"6", "status":"inactive", ..., "_timeCreated":"2014-04-14 14:44:49", "_timeChanged ":"2014-08-08 12:30:20"},
{"token":"2-token","prio":"8","status":"inactive",...}
] }
}
}
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
令牌。
我错过了什么?TIA