How can I use Jersey libraries to convert a JSON string to something more parseable in Java? -
i new rest , trying mess around bit accustomed it. have decided create little project involves accessing location data google's api.
there lot of posts converting json strings objects in java of them seem use different libraries i'm using, jersey. right now, i'm using google maps api return information location given user, right now, returns string so:
{ "results" : [ { "address_components" : [ { "long_name" : "san antonio", "short_name" : "san antonio", "types" : [ "locality", "political" ] }, { "long_name" : "bexar county", "short_name" : "bexar county", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "texas", "short_name" : "tx", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "united states", "short_name" : "us", "types" : [ "country", "political" ] } ], "formatted_address" : "san antonio, tx, usa", "geometry" : { "bounds" : { "northeast" : { "lat" : 29.73872, "lng" : -98.22295799999999 }, "southwest" : { "lat" : 29.2241411, "lng" : -98.8058509 } }, "location" : { "lat" : 29.4241219, "lng" : -98.49362819999999 }, "location_type" : "approximate", "viewport" : { "northeast" : { "lat" : 29.73872, "lng" : -98.22295799999999 }, "southwest" : { "lat" : 29.2241411, "lng" : -98.8058509 } } }, "place_id" : "chijrw7qbk9yxiyrvbagedvhvgg", "types" : [ "locality", "political" ] } ], "status" : "ok" }
i need have way parse kind of information, , know java has hash tables built in. there easy way convert information? possibly hash table of sort? please include usage of whatever object convert to, can see exact way need parse information. here relevant part of code have written far.
client client = clientbuilder.newclient(); webtarget target = client.target("https://maps.googleapis.com/maps/api/geocode/json?address=" + loc + "&sensor=false"); string result = target.request(mediatype.text_plain).get(string.class);
thanks can out :)
there lot of posts converting json strings objects in java of them seem use different libraries i'm using, jersey.
based on this, assuming try convert json in java objects. if want go ahead , store of member variables in hashmaps, can if want to.
so first of all, need following dependencies project:
<dependency> <groupid>org.glassfish.jersey.core</groupid> <artifactid>jersey-client</artifactid> <version>2.25.1</version> </dependency> <dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-json-jackson</artifactid> <version>2.25.1</version> </dependency>
the error in current code expect json, not text/plain. not want string object, want corresponding pojo. might this:
string loc = "new york"; client client = clientbuilder.newclient(); client.register(mapperprovider.class); webtarget target = client.target("https://maps.googleapis.com/maps/api/geocode/json?address=" + urlencoder.encode(loc, "utf-8") + "&sensor=false"); googlemapsresponse result = target.request(mediatype.application_json).get(googlemapsresponse.class);
the mapperprovider required enforce naming strategy, json's snake_case java's camelcase, , looks this:
@provider public class mapperprovider implements contextresolver<objectmapper> { final objectmapper mapper; public mapperprovider() { mapper = new objectmapper(); mapper.setpropertynamingstrategy( propertynamingstrategy.snake_case); } public objectmapper getcontext(class<?> aclass) { return mapper; } }
the start of googlemaps class is
public class googlemapsresponse { public result[] results; public string status; }
result has contain addresscomponent array, addresscomponent class contains string longname, string shortname type array etc.
this model , implement
Comments
Post a Comment