这是我的 JSON 数组:
[
[ 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();