ahmettekeli
ahmettekeli

Reputation: 3

jsonconvert.deserializeobject returns null

I'm trying to get coordinates from google maps when user enters 2 values of the address (for now) like city and street.Having trouble with deserialization of the Json string which comes from google maps api.Must be pretty simple please help me out about what i am missing.

Here is the json string: http://pasted.co/d9e7c1de

i need results/geometry/location/lat, results/geometry/location/lng info

and here is my code:

public class Geometry
{
    [JsonProperty("bounds")]
    public Bounds bounds { get; set; }
    [JsonProperty("location")]
    public Location location { get; set; }
    [JsonProperty("location_type")]
    public string location_type { get; set; }
    [JsonProperty("viewport")]
    public Viewport viewport { get; set; }
}

public class Result
{
    public List<AddressComponent> address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public bool partial_match { get; set; }
    public string place_id { get; set; }
    public List<string> types { get; set; }
}

protected void Button1_Click(object sender, EventArgs e)
{
    double  coordinatesX = 0;
    double coordinatesY = 0;
    string APIKEY = "****************************";
    string MyAdres = TextBox1.Text + "," + TextBox2.Text;
    string stringpath;
    stringpath = "https://maps.googleapis.com/maps/api/geocode/json?address=" + MyAdres + "&key=" + APIKEY;
    WebClient Web = new WebClient();
    string Jsonstring = Web.DownloadString(stringpath).ToString();
    Result m = JsonConvert.DeserializeObject<Result>(Jsonstring);

    coordinatesX = m.geometry.location.lat;
    coordinatesY = m.geometry.location.lng;
}

Upvotes: 0

Views: 5511

Answers (1)

Denis Krasakov
Denis Krasakov

Reputation: 1578

You need to use another top-level class to deserialize responce Try this:

public class Responce
{
    public string status{get;set;}
    public List<Result> results {get;set;}
}
...
var responce = JsonConvert.DeserializeObject<Responce>(Jsonstring);
DoSomething(responce.results);

Upvotes: 2

Related Questions