jsonconvert.deserializeobject returns null

本文关键字:null returns deserializeobject jsonconvert | 更新日期: 2023-09-27 18:18:10

当用户输入地址(目前)的2值时,我试图从谷歌地图获得坐标,如城市和街道。在反序列化来自google maps api的Json字符串时遇到麻烦。一定很简单,请帮助我,我错过了什么。

下面是json字符串:http://pasted.co/d9e7c1de

i need results/geometry/location/lat,结果/几何/位置/液化天然气信息

下面是我的代码:
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;
}

jsonconvert.deserializeobject returns null

您需要使用另一个顶级类来反序列化响应试试这个:

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