jsonconvert.deserializeobject返回null [英] jsonconvert.deserializeobject returns null

查看:687
本文介绍了jsonconvert.deserializeobject返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入2个地址值(暂时)(例如城市和街道)时,我正在尝试从Google地图获取坐标.对来自Google Maps API的Json字符串进行反序列化遇到麻烦.请务必非常简单帮助我了解我所缺少的东西.

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.

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

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

我需要结果/几何形状/位置/纬度, 结果/几何/位置/lng信息

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

这是我的代码:

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;
}

推荐答案

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

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);

这篇关于jsonconvert.deserializeobject返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆