0

我有一个 Json 文本,在下面我得到两个相似的名称,用于所需的字段名称“localscope”

{"processingTime":"0.002522",
 "version":"1.6.0.801 build 120621",
 "documentLength":"22",
 "document":{
   "administrativeScope":{
     "woeId":"23424848",
     "type":"Country",
     "name":"India",
     "centroid":{
       "latitude":"21.7866","longitude":"82.7948"}
     },
     "geographicScope":{
       "woeId":"23424848","type":"Country","name":"India","centroid":{"latitude":"21.7866","longitude":"82.7948"}},
        "localScopes":[{"localScope":{"woeId":"55953775","type":"Suburb","name":"Vinod Nagar, Rajkot, Gujarat, IN (Suburb)","centroid":{"latitude":"22.2554","longitude":"70.8101"},"southWest":{"latitude":"22.2463","longitude":"70.8003"},"northEast":{"latitude":"22.2645","longitude":"70.8199"},
         "ancestors":[{"ancestor":{"woeId":"2295404","type":"Town","name":"Rajkot"}},{"ancestor":{"woeId":"12586430","type":"District","name":"Rajkot"}},{"ancestor":{"woeId":"2345743","type":"State","name":"Gujarat"}},{"ancestor":{"woeId":"23424848","type":"Country","name":"India"}}]}},{"localScope":{"woeId":"23424848","type":"Country","name":"India (Country)","centroid":{"latitude":"21.7866","longitude":"82.7948"},"southWest":{"latitude":"6.7471","longitude":"68.0323"},"northEast":{"latitude":"36.2617","longitude":"97.403"},"ancestors":"\n"}}],"extents":{"center":{"latitude":"22.2554","longitude":"70.8101"},"southWest":{"latitude":"6.7471","longitude":"68.0323"},"northEast":{"latitude":"36.2617","longitude":"97.403"}},"0":{"placeDetails":{"placeId":"1","place":{"woeId":"23424848","type":"Country","name":"India","centroid":{"latitude":"21.7866","longitude":"82.7948"}},"placeReferenceIds":"1","matchType":"0","weight":"1","confidence":"10"}},"1":{"placeDetails":{"placeId":"2","place":{"woeId":"55953775","type":"Suburb","name":"Vinod Nagar, Rajkot, Gujarat, IN","centroid":{"latitude":"22.2554","longitude":"70.8101"}},"placeReferenceIds":"2","matchType":"0","weight":"1","confidence":"10"}},
   "referenceList":[
    {"reference":{
     "woeIds":"23424848",
     "placeReferenceId":"1",
     "placeIds":"1",
      "start":"17",
      "end":"22",
      "isPlaintextMarker":"1",
      "text":"india",
      "type":"plaintext",
      "xpath":""}
     },{
     "reference":{
      "woeIds":"55953775",
      "placeReferenceId":"2",
      "placeIds":"2",
      "start":"5",
      "end":"16",
      "isPlaintextMarker":"1",
      "text":"vinod nagar",
      "type":"plaintext",
      "xpath":""
    }
   }
  ]
 }
}

现在我试图找出 localscope 但由于有两个 localscope 作为字段,我的以下代码没有给我想要的结果

private boolean parseLocal(JsonParser jp, PlaceMakerObject obj) throws IOException {

        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldname = jp.getCurrentName();   
 if ("localscope".equalsIgnoreCase(fieldname)) {
                while (jp.nextToken() != JsonToken.END_OBJECT) {
                    String fieldname2 = jp.getCurrentName();
                   // System.out.println(fieldname2);
                    if ("woeid".equalsIgnoreCase(fieldname2)) {
                        jp.nextToken();
                        obj.setWoeid(jp.getText());
                    }

                    if ("type".equalsIgnoreCase(fieldname2)) {
                        jp.nextToken();
                        System.out.println(jp.getText());
                        obj.setType(jp.getText());
                    }

因此,当我打印类型时,它会从两个 localscopes 打印郊区和国家/地区的字段,但是我只想要一个。

4

1 回答 1

0

为什么要使用 Streaming API 而不是数据绑定?只需将 JSON 绑定到 POJO(您可以构建类,省略您不关心的字段,配置映射器以忽略未知属性),然后选择您想要的值。

于 2012-09-21T21:23:35.723 回答