[SalesForce] JSON response parser returning some fields null

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:

Serialized storage: (storage:[geometry=myGeometry:[location=myLocation:[lat=41.156457, lng=-64.730457]], id=7c0ddc371afea935f81863ddfd9ebc1e813121b2, name=Establishment, place_id=ChIJ_bErEeHjt4kRlErHPKNZjAA, vicinity=7077 Main St, Chicago])

public class myLocation
{
    public Double lat {get;set;}
    public Double lng {get;set;}
    public myLocation(Double lat, Double lng)
    {
        this.lat = lat;
        this.lng = lng;
    }
}

public class myGeometry
{
    public myLocation location {get;set;}
    public myGeometry(myLocation location)
    {
        this.location = location;
    }
}

public class storage 
{
    public myGeometry geometry {get;set;}
    public String id {get;set;}
    public String name {get;set;}
    public String vicinity {get;set;}
    public String place_id {get;set;}

    public storage(myGeometry geometry, String id, 
                    String name, String vicinity, String place_id)
    {
        this.geometry = geometry;
        this.id = id;
        this.name = name;
        this.vicinity = vicinity;
        this.place_id = place_id;
    }
}

List<storage> storagelist = new List<storage>();
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)
            {
                storage sto = (storage)parser.readValueAs(storage.class);
                storagelist.add(sto);
                System.debug('*****Serialized storage: ' + storagelist);
            }
        }
    }
}
Related Topic