摘要:使用Google API 查詢地址(C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using System.Web.Script.Serialization;
using System.IO;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static object[] GetTransformData(string jsonText)
{
JavaScriptSerializer s = new JavaScriptSerializer();
Dictionary JsonData = (Dictionary)s.DeserializeObject(jsonText);
Dictionary responseData = (Dictionary)JsonData["responseData"];
var results = responseData["results"] as object[];
return results;
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpWebRequest objHttpWebReq = null;
StreamReader responseReader = null;
try
{
string searchStr = TextBox1.Text;
string url = string.Format("http://www.google.com/uds/GlocalSearch?q={0}&v=1.0&nocache=1298223400032", searchStr);
objHttpWebReq = WebRequest.Create(url) as HttpWebRequest;
objHttpWebReq.Method = "GET";
objHttpWebReq.ContentType = "application/json";
responseReader = new StreamReader(objHttpWebReq.GetResponse().GetResponseStream());
var responseData = responseReader.ReadToEnd();
var a = GetTransformData(responseData);
Response.Write("返回條數:" + a.Length + "
");
foreach (Dictionary x in a)
{
var lst = new List();
if (x["country"] != null)
{
lst.Add(x["country"].ToString());
}
if (x["region"] != null)
{
lst.Add(x["region"].ToString());
}
if (x["city"] != null)
{
lst.Add(x["city"].ToString());
}
if (x["streetAddress"] != null)
{
lst.Add(x["streetAddress"].ToString());
}
Response.Write(string.Join(",",lst.ToArray()));
Response.Write("
");
}
}
finally
{
objHttpWebReq.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
}
}
人生到處知何似
應似飛鴻踏雪泥