c#地理编码只读取json字符串中的第一个数组

本文关键字:字符串 第一个 数组 json 读取 编码 | 更新日期: 2023-09-27 18:07:05

我目前正在编写一个方法来地理编码我的邮政编码,并将它们存储在我的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();

                }

            }
        }

c#地理编码只读取json字符串中的第一个数组

我设法解决了这个问题:

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;    
            }


        }