0

Json text があります。以下では、目的のフィールド名「localscope」に対して2つの同様の名前を取得します

{"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":""
    }
   }
  ]
 }
}

今、私はローカルスコープを見つけようとしていますが、フィールドとして2つのローカルスコープがあるため、次のコードでは望ましい結果が得られません

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 からフィールドを取得して suburb と country の両方が印刷されますが、必要なのは 1 つだけです。

4

1 に答える 1

0

なぜデータ バインディングではなくストリーミング API を使用しているのですか? JSON を POJO にバインドするだけで (クラスをスケルトン化し、不要なフィールドを除外し、不明なプロパティを無視するようにマッパーを構成できます)、必要な値を選択します。

于 2012-09-21T21:23:35.723 に答える