Java GSON解析器在json上循环时跳过对象

Java GSON解析器在json上循环时跳过对象,java,json,gson,Java,Json,Gson,我有一个Json数组,可以解析为GSON。它有以下内容: [ { "type": "way", "id": 70215497, "nodes": [ 838418570, 838418571 ] }, { "type": "way", "id": 70215500, "nodes": [ 838418548, 838418626 ] } ] 我尝试使用以下代码示例对其进行解析: while (jsonReader.hasN

我有一个Json数组,可以解析为GSON。它有以下内容:

[
{
  "type": "way",
  "id": 70215497,
  "nodes": [
    838418570,
    838418571
  ]
},
{
  "type": "way",
  "id": 70215500,
  "nodes": [
    838418548,
    838418626
  ]
}
]
我尝试使用以下代码示例对其进行解析:

    while (jsonReader.hasNext()){
                Element type = gson.fromJson(jsonReader, Element.class);
                if (type.GetType().contentEquals("way")) {
                    Way way = gson.fromJson(jsonReader, Way.class);
                    System.out.println(way.GetId());                
                }
     }
其中,
元素
仅为

public class Element {
    private String type;
    public String GetType() {
        return type;
    }
}
方式

public class Way {
    private long id;
    private String type;
    List<Long> nodes;       
    public long GetId() {
        return id;
    }
}
公共类方式{
私人长id;
私有字符串类型;
列出节点;
公共长GetId(){
返回id;
}
}
现在由于某种原因,只有
70215500
会打印出来。在实际代码中,其他一些元素也会发生这种情况。为什么呢


编辑:它基本上只读取1/2对象。为什么?

您不需要先阅读
元素
类,然后再阅读
方式
类。阅读
Way
并检查其类型:

try (JsonReader jsonReader = new JsonReader(new FileReader(jsonFile))) {
    jsonReader.beginArray();
    while (jsonReader.hasNext()) {
        Way way = gson.fromJson(jsonReader, Way.class);
        if (way.getType().contentEquals("way")) {
            System.out.println(way.getId());
        }
    }
}

上面的代码应该打印所有的
id
s.

看起来您只是打印出id.“System.out.println(way.GetId());”我是。我的问题是为什么它会跳过另一个它不会跳过另一个,你只想打印出身份证这就是为什么它只返回身份证。那你是什么意思?它应该转到类型为“way”的下一个对象,并打印其id?请编辑代码示例以创建一个具有当前和预期输出的对象。我将尝试这样做。我猜读取的元素正在消耗我的元素。。。