0
我试图让使用使用谷歌地图API的邮政编码地址。到目前为止,我可以从响应中获取JSON数据并将其存储在“var”中。提取数据
但是我不能提取的字段如街道,镇,县 - >理想情况下为针对每个单独的字符串变量。
这是我的代码到目前为止。
string postcode = Convert.ToString(tbPostcode.Text);
string searchCode = "http://maps.googleapis.com/maps/api/geocode/json?address=" + postcode + "&sensor=false";
var result = "";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(searchCode);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write("");
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
MessageBox.Show(result);
希望有人能帮忙,谢谢!
亚当
tyhe C#类如果你不介意使用第三方库,我觉得'JSON.NET'成为一个好的图书馆解析JSON数据http://james.newtonking.com/projects/json-net.aspx – keyboardP 2013-03-23 13:18:44
你认为用XML重新获取数据会更容易吗? – Adam92 2013-03-23 13:25:20
不一定。返回的JSON并不是特别难以解析,尤其是在提到'JSON.NET'或'DataContractJsonSerializer'类的情况下。即使使用XML,您也必须解析数据,因此更适合您使用哪种格式。 – keyboardP 2013-03-23 13:27:22