1

这是我的 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();
4

1 回答 1

2

如果 hasNext() 返回 false,您不想调用 skipValue() 或 next*。GSON文档建议先累积值。该主题的一个变体是:

reader.beginArray();
while (reader.hasNext()) {
    int position;
    int a, b, d; // stores your parsed values
    String c; // stores your parsed values
    reader.beginArray(); 
    // read and parse first four elements, checking hasNext() each time for robustness
    for (position = 0; position < 4 && reader.hasNext(); ++ position) {
        if (position == 0) a = reader.nextInt(); 
        else if (position == 1) b = reader.nextInt();
        else if (position == 2) c = reader.nextString();
        else if (position == 3) d = reader.nextInt();
    } 
    // if position < 4 then there weren't enough values in array.
    if (position == 4) { // correctly read 
        System.out.println(a + " " + b + " " + c + " " + d);
    }
    // skip rest of array, regardless of number of values
    while (reader.hasNext())
        reader.skipValue();
    reader.endArray();
} 
reader.endArray();

请注意,还有很多其他方法可以解析前 4 个值,使用对您的情况有意义的任何方法(例如,首先将它们存储在 List 中,或者将它们存储为字符串,然后再解析,或者您想要的任何方法——重点是,不要对数组中的元素数量做出假设,遵守规则并在读取每个元素之前使用 hasNext() )。

于 2013-08-04T18:09:43.550 回答