我有一個JSON,如:GSON:END_OBJECT永遠不會叫
{ 「ADATA」:
{ 「比賽」:
{ 「新」:
{「token」:「1-token」,「prio」:「6」,「status」:「inactive」,...,「_timeCreated」:「2014-04-14 14:44: 49「,」_tim eChanged「:」2014-08-08 12:30:20「},
{」token「:」2-token「,」prio「:」8「,」status「:」inactive「,.. }
]}
}
}
JSON文件是8MB大,是有效的,因爲它可以使用默認的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
我在json中識別(非json)數組時遇到了問題,那就是方括號數組。我發現我必須爲包含MyClass []數組類的json api響應創建一個類。這可能是問題嗎? – TTransmit
爲什麼'[]'數組不是json? – injecteer
還有一種在json中構造數組數據的另一種方法,我在一些不使用方括號的項目中看到過使用這種方法。我總是稱這是一個json數組和方括號數組,我只是稱其爲數組。看起來這可能只是我作出這種區分。爲了讓我的答案更清楚,我說得不太清楚,對不起。 – TTransmit