简体   繁体   中英

c# geocoding it only reads the first array in the json string

i am currently writing a method to geocode my postal codes and storing them in my sql database Below is an example of my codes:

Problem: There are two arrays in the 'json' string. However, when i try to geocode, it only geocode the postal code in the first array and not the second. How should I go about doing to geocode the postal code in the second array as well.

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();


                }


            }

        }

I managed to solve this:

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




        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM