1

我目前正在编写一种方法来对我的邮政编码进行地理编码并将它们存储在我的 sql 数据库中以下是我的代码示例:

问题:“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 =@lat,long =@long where postalCode =@postal";
                    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();


                }


            }

        }
4

1 回答 1

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 =@lat,long =@long where postalCode =@postal";
                    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;    
            }




        }
于 2013-04-07T14:14:30.183 回答