How to distinguish out of similar fieldname returned as parsed json object Jackson Json parser?

I have a Json text , in the following I get two similar names for the desired 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":""
    }
   }
  ]
 }
}

Now I am trying to find out localscope but since there are two localscope as field my following code is not giving me the desired result

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());
                    }

So when I print type it prints both suburb and country taking fields from both localscopes , however I just want one.

Answers


Why are you using Streaming API instead of data-binding? Just bind JSON into POJOs (you can skeletal classes, leave out fields you don't care, configure mapper to ignore unknown properties), and pick values you want.


Need Your Help

Delete user score as an App Admin with Graph API Explorer

facebook facebook-graph-api facebook-apps facebook-access-token

Is there a way of deleting a specific user Score from the Graph API Explorer?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.