2013-04-02 45 views
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(); 


       } 


      } 

     } 

回答

0

我設法解決這個問題:

protected void getGeoCoding() 
     { 
      string postalCode = string.Empty; 
      string glat = string.Empty; 
      string glong = string.Empty; 
      string gPostal = string.Empty; 
      string sqlUpdateQuery = string.Empty; 
      string sqlGetQuery = string.Empty; 

      string json = DAO.getNullLatLong(); 
      JObject jobject = JObject.Parse(json); 
      JArray items = (JArray)jobject["e"]; 

      JObject item; 
      JToken jtoken; 

      string connectionString = DAO.GetConnectionString(); 
      SqlConnection sqlConn = new SqlConnection(connectionString); 
      sqlConn.Open(); 


      for (int i = 0; i < items.Count; i++) //loop through rows 
      { 
       item = (JObject)items[i]; 
       jtoken = item.First; 

       while (jtoken != null)//loop through columns 
       { 

       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 += postalCode; 

        sqlUpdateQuery = "update latlongDB set lat [email protected],long [email protected] where postalCode [email protected]"; 
        SqlCommand updateCommand = new SqlCommand(sqlUpdateQuery, sqlConn); 
        updateCommand.Parameters.Add("@lat", SqlDbType.NVarChar).Value = glat; 
        updateCommand.Parameters.Add("@long", SqlDbType.NVarChar).Value = glong; 
        updateCommand.Parameters.Add("@postal", SqlDbType.NVarChar).Value = gPostal; 
        updateCommand.ExecuteNonQuery(); 
       } 

      } 


       jtoken = jtoken.Next;  
      } 




     }