0

I ' m using the geocode API's Google, I can get the JSON string with all dates , but when i will convert it in a object, it do not work, it return anything,I'm using the JSON.NET, i'm doing something wrong ?

in all JSON data, I wanna just take the formatted_address.

json request : http://maps.googleapis.com/maps/api/geocode/json?address=av.paulista&sensor=false%22

sorry my bad English

my main form: getting the JSON data(working)

private void btnConsumir_Click(object sender, EventArgs e)
    {
        string address = txtAddress.Text ;

        string searchCode = "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false";
        var JSONdata = "";


        var httpWebRequest = (HttpWebRequest)WebRequest.Create(searchCode);
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";


        var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            JSONdata = streamReader.ReadToEnd();
        }

        lblJSON.Text = JSONdata;//its a label

here i want get the formatted_address information on json:

[...]
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            JSONdata = streamReader.ReadToEnd();
        }

        lblJSON.Text = JSONdata;//its a label

        AddressObject addressObject = JsonConvert.DeserializeObject<addressObject>(JSONdata);
        string result = addressObject.formatted_address;

        lblJSON.Text = result;

and this is my class object:

class AddressObject
    {
        public string formatted_address { get; set; }




    }

thank you so much!

1 Answer 1

6

The results that come back from the api are much more than formatted_address. You need to deserialize the entire graph, then pluck out what you want.

With the class structure below, you can do this:

Rootobject mapdata = JsonConvert.DeserializeObject<Rootobject>(JSONdata);

...

public class Rootobject
{
    public Result[] results { get; set; }
    public string status { get; set; }
}

public class Result
{
    public Address_Components[] address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public string[] types { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Location
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Northeast
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Southwest
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Address_Components
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.