解析這是我的JSON數組:循環不打破 - JSON與GSON
[
[ 36,
100,
"The 3n + 1 problem",
56717,
0,
1000000000,
0,
6316,
0,
0,
88834,
0,
45930,
0,
46527,
5209,
200860,
3597,
149256,
3000,
1
],
[
........
],
[
........
],
.....// and almost 5000 arrays like above
]
我想需要EAH陣列的前四個值並跳過值的其餘部分,如:
36 100 "The 3n + 1 problem" 56717
這是我寫到目前爲止代碼:
reader.beginArray();
while (reader.hasNext()) {
reader.beginArray();
while (reader.hasNext()) {
System.out.println(reader.nextInt() + " " + reader.nextInt()
+ " " + reader.nextString() + " "
+ reader.nextInt());
for (int i = 0; i < 17; i++) {
reader.skipValue();
}
reader.skipValue();
}
reader.endArray();
System.out.println("loop is break"); // this is not printed as the inner loop is not breaking
}
reader.endArray();
reader.close();
而且如我所料正在打印:
36 100 "The 3n + 1 problem" 56717
.................................
..................................
1049 10108 The Mosquito Killer Mosquitos 49
1050 10109 Solving Systems of Linear Equations 129
1051 10110 Light, more light 9414
1052 10111 Find the Winning Move 365
這是行得通的,但內循環沒有正確地分解。我的代碼有什麼問題?我錯過了什麼,以便我的代碼無法正常工作?
編輯:(解決方案) 我結束了此解決方案:
reader.beginArray();
while (reader.hasNext()) {
reader.beginArray();
// read and parse first four elements, checking hasNext() each time for robustness
int a = reader.nextInt();
int b = reader.nextInt();
String c = reader.nextString();
int d = reader.nextInt();
System.out.println(a + " " + b + " " + c + " " + d);
while (reader.hasNext())
reader.skipValue();
reader.endArray();
}
reader.endArray();
請問您可以指定您正在使用Java讀取json的庫。 –
這是'谷歌'谷歌 –