Hi I have this response from google places api
"address_components": [
{
"long_name": "123",
"short_name": "123",
"types": ["street_number"]
},
{
"long_name": "Mulgoa Road",
"short_name": "Mulgoa Rd",
"types": ["route"]
},
{
"long_name": "Penrith",
"short_name": "Penrith",
"types": ["locality", "political"]
},
{
"long_name": "City of Penrith",
"short_name": "City of Penrith",
"types": ["administrative_area_level_2", "political"]
},
{
"long_name": "New South Wales",
"short_name": "NSW",
"types": ["administrative_area_level_1", "political"]
},
{
"long_name": "Australia",
"short_name": "AU",
"types": ["country", "political"]
},
{
"long_name": "2750",
"short_name": "2750",
"types": ["postal_code"]
}
],
Expected response
{
"postcode": "4215",
"unit_number": null,
"floor_number": null,
"building_name": null,
"building_number": null,
"block_number": null,
"street_number": "123",
"street_name": "Nerang",
"street_type": "Street",
"state": "Queensland",
"suburb": "Southport",
"city": null,
"district": null,
"fullAddress": "123 Nerang St, Southport QLD 4215, Australia"
}
On the basis of types array I want to populate values into response format.
This is what how I trying to implement it right now.
JsonNode resultData = new ObjectMapper().readTree(String.valueOf(response.getResponseBody()));
if (Objects.equals(resultData.get("status").asText(), "OK")) {
JsonNode addressComponentsArray = resultData.get("result").get("address_components");
LocationAddressComponent responseObject = new LocationAddressComponent();
if (addressComponentsArray.isArray()) {
for (JsonNode jsonNode : addressComponentsArray) {
Array res = jsonNode.get("types");
}
}
Can you please suggest how it can be done using stream or Jackson.
LocationComponentAddress.java
public class LocationAddressComponent {
String postcode;
String unit_number;
String floor_number;
String building_name;
String building_number;
String block_number;
String street_number;
String street_name;
String street_type;
String state;
String suburb;
String city;
String district;
String fullAddress;
}
This implementation only maps a few of the properties in the Google Places API response to the LocationAddressComponent object. You may need to add more mapping logic depending on your requirements.
if (addressComponentsArray.isArray()) {
for (JsonNode jsonNode : addressComponentsArray) {
ArrayNode types = (ArrayNode) jsonNode.get("types");
if (types.contains("street_number")) {
responseObject.setStreet_number(jsonNode.get("long_name").asText());
} else if (types.contains("route")) {
responseObject.setStreet_name(jsonNode.get("long_name").asText());
} else if (types.contains("locality")) {
responseObject.setSuburb(jsonNode.get("long_name").asText());
} else if (types.contains("administrative_area_level_1")) {
responseObject.setState(jsonNode.get("long_name").asText());
} else if (types.contains("postal_code")) {
responseObject.setPostcode(jsonNode.get("long_name").asText());
}
}
}
Related
Now i take JsonObject from API like this:
Its XML object converted to JsonObject.
"Details": {
"row": [
{
"item": [
{
"name": "Account",
"value": 12521512
},
{
"name": "ACCNO",
"value": 4214
},
{
"name": "Number",
"value": 5436
}
]
},
"item": [
{
"name": "Account",
"value": 5789678
},
{
"name": "ACCNO",
"value": 6654
},
{
"name": "Number",
"value": 0675
}
]
},
But i need convert this object and send like this:
{
"Details": {
"row": [
{
"Account": 12521512,
"ACCNO": 4214,
"Number": 12412421
},
{
"Account": 5789678,
"ACCNO": 6654,
"Number": "0675"
}
]
}
}
I have rows more than 1000, i need faster way to handle.
How to handle, please help me
You could use the JSON-java library to parse your input and transform it to your desired format. Something like this works but you may need to adjust it to your needs:
JSONObject jsonObject = new JSONObject(json); // Load your json here
JSONObject result = new JSONObject("{\"Details\": {\"row\": []}}");
for (Object row : jsonObject.getJSONObject("Details").getJSONArray("row")) {
if (!(row instanceof JSONObject)) continue;
Map<Object, Object> values = new HashMap<>();
for (Object item : ((JSONObject) row).getJSONArray("item")) {
if (!(item instanceof JSONObject)) continue;
values.put(((JSONObject) item).get("name"), ((JSONObject) item).get("value"));
}
result.getJSONObject("Details").getJSONArray("row").put(values);
}
// Now result is in your format
What is the cheapest way to take the jsonstring and hashmap below and produce the result in JAVA?
pseudocode data example:
String jsonstring = {
"people": {
"name": "name1",
},
"addresses": {
"address1": {
"number": "1234",
"city": "europa"
}
}
}
HashMap hashmap = {
["string.a.1"] = "stringa1",
["string.a.2"] = "stringa2",
["object.a.1"] = "{/"item1/":/"value1/"}" // serialized JSON object
}
String result = {
"people": {
"name": "name1",
},
"addresses": {
"address1": {
"number": "1234",
"city": "europa"
}
},
"buffer": {
"string.a.1": "stringa1",
"string.a.2": "stringa2",
"object.a.1": {
"item1": "value1"
}
}
}
Thanks in advance for any help.
I want to parse this json in android. Can you help me how to parse, if i need to start from head to parse or from results for this I don't know. Can you help me
{
"head":
{
"link": [],
"vars": ["city", "country"]
},
"results":
{
"distinct": false,
"ordered": true,
"bindings":
[
{
"city":
{
"type": "uri",
"value": "http://dbpedia.org/resource/Ferizaj"
} ,
"country":
{
"type": "uri",
"value": "http://dbpedia.org/resource/Kosovo"
}
}
]
}
}
Okay, first of all the JSON string you've given is invalid. I've written my answer based on the correct version of the JSON string that you have provided
{
"head":
{
"link": [],
"vars": ["city", "country"]
},
"results":
{
"distinct": false,
"ordered": true,
"bindings":
[
{
"city":
{
"type": "uri",
"value": "http://dbpedia.org/resource/Ferizaj"
} ,
"country":
{
"type": "uri",
"value": "http://dbpedia.org/resource/Kosovo"
}
}
]
}
}
Now, to get the "values", you'll need to do this.
JSONObject jsonObject = new JSONObject(response);
JSONObject results = jsonObject.getJSONObject("results");
JSONArray bindings = results.getJSONArray("bindings");
for (int i = 0; i < bindings.length(); i++) {
JSONObject binding = bindings.getJSONObject(i);
JSONObject city = binding.getJSONObject("city");
// Get value in city
String cityValue = city.getString("value");
Log.d("CITY", cityValue);
JSONObject country = binding.getJSONObject("country");
// Get value in country
String countryValue = country.getString("value");
Log.d("COUNTRY", countryValue);
}
i have trouble in parsing json
I have a nested json string, i need to convert to java object , the string look like this, i want to ask how to handle this nested dynamic json using jackson, how to decode dynamic nested json string
{
"resultCode": "0",
"dataObject": [
{
"lastSyncDate": "20140101000000",
"count": 2,
"refType": "ADO",
"update": [
{
"artist": "C",
"albumTitle": "道",
"productTitle": "道",
"thumbnail": "http://w.com/mposter/album/m/VACL00020880A_m.jpg",
"lastSyncDate": "20140425120159",
"refId": "VACL00214522"
},
{
"artist": "楊",
"albumTitle": "學",
"productTitle": "美",
"thumbnail": "http://m.jpg",
"lastSyncDate": "20140324161831",
"refId": "VACP00168673"
}
],
"delete": [ ]
},
{
"lastSyncDate": "20140101000000",
"count": 8,
"refType": "PAT",
"update": [
{
"artist": "方",
"thumbnail": "http://t.com/moov/images/profile/PAT/8/70/00021870_tn_1_s.jpg",
"lastSyncDate": "20140201010203",
"refId": "00021870"
},
{
"artist": "楊",
"lastSyncDate": "20140328120831",
"refId": "00000125"
},
{
"artist": "陳",
"thumbnail": "http://s.jpg",
"lastSyncDate": "20140328185030",
"refId": "00017704"
}
],
"delete": [ ]
},
{
"lastSyncDate": "20140101000000",
"count": 4,
"refType": "PAB",
"update": [
{
"artist": "陳",
"albumTitle": "The Key",
"thumbnail": "http:/m.jpg",
"lastSyncDate": "20140603143528",
"refId": "VAUN00031629A"
},
{
"artist": "何",
"albumTitle": "梁",
"thumbnail": "http://m.jpg",
"lastSyncDate": "20140603143528",
"refId": "VAEA00003170A"
},
{
"artist": "何",
"albumTitle": "艷",
"thumbnail": "http://m.jpg",
"lastSyncDate": "20110603151452",
"refId": "VAEA00003179A"
}
],
"delete": [ ]
},
{
"lastSyncDate": "20140101000000",
"count": 4,
"refType": "PP",
"update": [
{
"chiName": "其",
"engName": "Other",
"lastSyncDate": "20140130010203",
"chiAuthor": "",
"engAuthor": "",
"refId": "PP1000000003"
},
{
"chiName": "演",
"engName": "E演",
"thumbnail": "http://s.jpg",
"lastSyncDate": "20140126010758",
"chiAuthor": "專",
"engAuthor": "Recommended",
"refId": "PP1000000040"
},
{
"chiName": "日本派台歌",
"engName": "Japan New Releases",
"lastSyncDate": "20140126010758",
"chiAuthor": "",
"engAuthor": "",
"refId": "PP1000000057"
},
{
"chiName": "9",
"engName": "9",
"thumbnail": "http://s.jpg",
"lastSyncDate": "20140126010203",
"chiAuthor": "專",
"engAuthor": "Recommended",
"refId": "PP1000000048"
}
],
"delete": [ ]
}
]
}
You only need to reproduce the structure of the json using java class structure. For instance, for your case:
public class Result {
String resultCode;
List<DataObject> dataObjects;
<GETTERS & SETTERS>
}
public class DataObject {
String lastSyncDate;
int count;
String refType;
List<Update> updates;
List<Delete> deletes;
<GETTERS & SETTERS>
}
public class Update {
String artist;
String albumTitle;
String productTitle;
String thumbnail;
String lastSyncDate;
String refId;
<GETTERS & SETTERS>
}
public class Delete {
String refId;
<GETTERS & SETTERS>
}
I assumed that Delete class only contains the refId. With this structure, you'll be able to map the json to an object of the class Result without any problems doing:
byte[] jsonData = <YOUR JSON>
ObjectMapper objectMapper = new ObjectMapper();
Result result = objectMapper.readValue(jsonData, Result.class);
Since the field of Update is not fixed, use map instead of the Update class.
public class Result {
String resultCode;
List<DataObject> dataObject;
<GETTERS & SETTERS>
}
public class DataObject {
String lastSyncDate;
int count;
String refType;
List<Map<String, String>> update;
List<String> delete;
<GETTERS & SETTERS>
}
Assume that update only contains String values.
String jsonData = "YOUR JSON";
ObjectMapper objectMapper = new ObjectMapper();
Result result = objectMapper.readValue(jsonData, Result.class);
I want to get longitude & latitude from a json. to do so i use the Google Maps API to get the json then i put it into a JsonElement but now i can't figure out how to read this values (latitude, longitude).
public static void getGeo(){
HttpResponse res = WS.url("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true").get();
int status = res.getStatus();
String type = res.getContentType();
JsonElement json = res.getJson();
JsonObject jsonObject = json.getAsJsonObject();
//with a system out i can see that the json is parsed correctly
System.out.print(jsonObject.get("results"));
}
the parsed json:
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"address_components": [ {
"long_name": "1600",
"short_name": "1600",
"types": [ "street_number" ]
}, {
"long_name": "Amphitheatre Pkwy",
"short_name": "Amphitheatre Pkwy",
"types": [ "route" ]
}, {
"long_name": "Mountain View",
"short_name": "Mountain View",
"types": [ "locality", "political" ]
}, {
"long_name": "California",
"short_name": "CA",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United States",
"short_name": "US",
"types": [ "country", "political" ]
}, {
"long_name": "94043",
"short_name": "94043",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 37.4219720,
"lng": -122.0841430
},
"location_type": "ROOFTOP",
"viewport": {
"southwest": {
"lat": 37.4188244,
"lng": -122.0872906
},
"northeast": {
"lat": 37.4251196,
"lng": -122.0809954
}
}
}
} ]
}
any suggestion please ?
This should do the trick for you:
HttpResponse res = WS.url("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true").get();
JsonElement json = res.getJson();
System.out.println("JSON: " + json);
//with a system out i can see that the json is parsed correctly
JsonArray jsArr = json.getAsJsonObject().getAsJsonArray("results");
for (JsonElement jsEl : jsArr)
{
JsonObject locationObject = jsEl.getAsJsonObject().getAsJsonObject("geometry").getAsJsonObject("location");
double lat = locationObject.getAsJsonPrimitive("lat").getAsDouble();
double lng = locationObject.getAsJsonPrimitive("lng").getAsDouble();
System.out.println("Lat is " + lat + " and lng is " + lng);
}