I am trying to parse a JSON response from google. All the fields are populating with the exception of 'lat' and 'lng'. Not sure what I need to do to get the coordinates. Any help is much appreciated.
JSON
"results" : [
{
"geometry" : {
"location" : {
"lat" : 41.156457,
"lng" : -64.730457
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping.png",
"id" : "7c0ddc371afea935f81863ddfd9ebc1e813121b2",
"name" : "Establishment",
"opening_hours" : {
"open_now" : false
},
"place_id" : "ChIJ_bErEeHjt4kRlErHPKNZjAA",
"price_level" : 2,
"rating" : 4.1,
"reference" : "CoQBdAAAAF6"
"scope" : "GOOGLE",
"types" : [ "store", "establishment" ],
"vicinity" : "7077 Main St, Chicago"
}
Parse the response:
JSONparser parser = JSON.createparser(Results);
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_ARRAY){
while(parser.nextToken() != null){
if(parser.getCurrentToken() == JSONToken.START_OBJECT){
storages sto = (storages)parser.readvalueas(storages.class);
storagelist.add(sto);
system.debug('*****Serialized storage: ' + storagelist);
}
}
}
}
Inner classes used to parse:
public class storages {
public string id {get;set;}
public string lat {get;set;}
public string lng {get;set;}
public string name {get;set;}
public string vicinity {get;set;}
public string place_id {get;set;}
public storages(string recordid, string lt, string lg, string nm, string address, string place){
id = recordid;
lat = lt;
lng = lg;
name = nm;
vicinity = address;
place_id = place;
}
}
Best Answer
You need to represent each inner object of the JSON, with an actual object, in your case it should be look something like this: debug output: