如何区分出类似的fieldname返回解析的json对象Jackson Json解析器?

时间:2012-09-21 11:11:43

标签: java json parsing jackson

我有一个Json文本,在下面我得到了两个相似的名字,用于所需的fiedname“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的郊区和国家/地区,但我只想要一个。

1 个答案:

答案 0 :(得分:0)

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