Deserializing Google Maps geocode

Jan 14, 2009 at 9:14 PM
Edited Jan 15, 2009 at 1:48 AM
Hi, 

I'm trying to deserialize the Google Maps geocoding JSON (http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct

I tried creating a class and just running serializer.Deserialize but it gave me this error: "Could not find member 'Status' on object of type 'RuntimeType'" 

I then tried to load it into a JObject as suggested in http://www.codeplex.com/Json/Thread/View.aspx?ThreadId=43497 but that seems a little tedious. 

Is there an easier/more automatic way to do this? 

-Trev
Jan 15, 2009 at 1:43 AM
Edited Jan 19, 2009 at 7:28 PM
Nevermind.  I was able to do it using JavaScriptSerializer.  

My code is below:

    WebClient client = new WebClient();
    String json = client.DownloadString(apiUrl);
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    GoogleMapGeocoderStructure jsonGoogleMapGeocoder = serializer.Deserialize<GoogleMapGeocoderStructure>(json);

    public class GoogleMapGeocoderStructure
    {
        public string Name;
        public Status Status;
        public List<Placemark> Placemark;
    }

    public class Status
    {
        public string Request;
        public string Code;
    }

    public class Placemark
    {
        public string Address;
        public AddressDetails AddressDetails;
        public Point Point;
    }

    public class AddressDetails
    {
        public int Accuracy;
        public Country Country;
    }

    public class Country
    {
        public string CountryNameCode;
        public AdministrativeArea AdministrativeArea;
    }

    public class AdministrativeArea
    {
        public string AdministrativeAreaName;
        public SubAdministrativeArea SubAdministrativeArea;
    }

    public class SubAdministrativeArea
    {
        public string SubAdministrativeAreaName;
        public Locality Locality;
    }

    public class Locality
    {
        public string LocalityName;
        public Thoroughfare Thoroughfare;
        public PostalCode PostalCode;
    }

    public class Thoroughfare
    {
        public string ThoroughfareName;
    }

    public class PostalCode
    {
        public string PostalCodeNumber;
    }

    public class Point
    {
        public List<decimal> Coordinates;
    }


Coordinator
Jan 15, 2009 at 7:42 AM
This will work for you:

JsonConvert.DeserializeObject<GoogleMapGeocoderStructure>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore });