2

我有一个像这样的 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

4

2 回答 2

3

hasNext()说“如果当前数组或对象有另一个元素,则返回 true。” 在您的情况下,当它到达对象或数组的末尾时,不会hasNext()false调用。case END_ARRAYcase END_OBJECT

这可能有效:

while( reader.hasNext() 
        || reader.peek() == END_ARRAY 
        || reader.peek() == END_OBJECT ){
    eachToken( reader );
    Log.i( TAG, "has next " + reader.hasNext() );
}

这与乔伊的回答基本相同,但不适合发表评论。

于 2015-01-06T16:32:03.523 回答
1

我想问题可能出在您处理数据的方式上

如果您查看JavaDocJsonReader它特别定义了您需要使用数组括号..

在数组处理方法中,首先调用 beginArray() 以使用数组的左括号。然后创建一个累加值的 while 循环,当 hasNext() 为 false 时终止。最后,通过调用 endArray() 读取数组的右括号。

hasNext()数组完成后也是假的......

于 2015-01-06T15:32:13.533 回答