使用 JSON.NET c# 将 JSON 字符串(地理编码 API 的谷歌)反序列化为对象

Ita*_*osé 0 c# json google-maps

我正在使用地理编码 API 的 Google,我可以获得所有日期的 JSON 字符串,但是当我将其转换为对象时,它不起作用,它返回任何内容,我正在使用 JSON.NET,我是做错了什么?

在所有 JSON 数据中,我只想获取 formatted_address。

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

对不起我的英语不好

我的主要形式:获取 JSON 数据(工作)

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
Run Code Online (Sandbox Code Playgroud)

在这里我想获取有关 json 的 formatted_address 信息:

[...]
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;
Run Code Online (Sandbox Code Playgroud)

这是我的类对象:

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




    }
Run Code Online (Sandbox Code Playgroud)

太感谢了!

Cro*_*der 6

从 api 返回的结果远不止formatted_address. 您需要反序列化整个图形,然后取出您想要的内容。

使用下面的类结构,您可以这样做:

Rootobject mapdata = JsonConvert.DeserializeObject<Rootobject>(JSONdata);
Run Code Online (Sandbox Code Playgroud)

...

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; }
}
Run Code Online (Sandbox Code Playgroud)