1
我目前正在寫的方法進行地址解析我的郵政編碼和將它們存儲在我的SQL數據庫 下面是我的代碼的示例:C#地理編碼它僅讀入JSON字符串第一陣列
問題:有'json'字符串中的兩個數組。但是,當我嘗試進行地址解析時,它僅對第一個數組中的郵政編碼進行地理編碼,而不是第二個。我該如何去做第二個數組中的郵政編碼。
protected void getGeoCode()
{
string json = DAO.getLatLongNull();
JObject jobject = JObject.Parse(json);
JArray array = (JArray)jobject["e"];
string postalCode = string.Empty;
string glat = string.Empty;
string glong = string.Empty;
string gPostal = string.Empty;
string sqlQuery = string.Empty;
string connectionString = DAO.GetConnectionString();
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
foreach (var item in array)
{
postalCode = item["postalCode"].ToString();
string url = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Singapore%20";
dynamic googleResults = new Uri(url + postalCode).GetDynamicJsonObject();
foreach (var result in googleResults.results)
{
glat += result.geometry.location.lat;
glong += result.geometry.location.lng;
gPostal += item["postalCode"].ToString();
sqlQuery = "update testlatlong set lat [email protected],long [email protected] where postalCode [email protected]";
SqlCommand command = new SqlCommand(sqlQuery, sqlConn);
command.Parameters.Add("@lat", SqlDbType.NVarChar).Value = glat;
command.Parameters.Add("@long", SqlDbType.NVarChar).Value = glong;
command.Parameters.Add("@postal", SqlDbType.NVarChar).Value = gPostal;
command.ExecuteNonQuery();
}
}
}