![](/img/trans.png)
[英]JsonConvert.DeserializeObject returns null in ActionResult Method
[英]jsonconvert.deserializeobject returns null
當用戶輸入2個地址值(暫時)(如城市和街道)時,我正在嘗試從Google地圖獲取坐標。對來自Google Maps API的Json字符串進行反序列化遇到麻煩。必須非常簡單,請幫幫我關於我所缺少的。
這是json字符串: http : //pasted.co/d9e7c1de
我需要結果/幾何形狀/位置/緯度,結果/幾何形狀/位置/ LNG信息
這是我的代碼:
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;
}
您需要使用另一個頂級類來反序列化響應請嘗試以下操作:
public class Responce
{
public string status{get;set;}
public List<Result> results {get;set;}
}
...
var responce = JsonConvert.DeserializeObject<Responce>(Jsonstring);
DoSomething(responce.results);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.