public static GeocodeJsonObject ReadJsonToObject(string json) { GeocodeJsonObject deserializedObject = new GeocodeJsonObject(); //MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)); //DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType()); //deserializedUser = ser.ReadObject(ms) as User; //ms.Close(); deserializedObject = JsonConvert.DeserializeObject <GeocodeJsonObject>(json); return(deserializedObject); }
//call geocode api to get long/lat public static async Task <GeocodeJsonModel.Location> LoadGeocode(string Address) { GeocodeJsonObject JsonGeo = null; string url = $"https://maps.googleapis.com/maps/api/geocode/json?address= {Address} &key=AIzaSyAp4h1UeIBiWDZvJpfG6tlQFvEZwcwpmLg"; HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url); if (response.IsSuccessStatusCode) { JsonGeo = await response.Content.ReadAsAsync <GeocodeJsonObject>(); return(JsonGeo.Results[0].Geometry.Location); } else { throw new Exception(response.ReasonPhrase); } }
/// <summary> /// Берет строки из таблицы, парсит JSON и раскидывает по столбцам /// </summary> public void ConvertJsonStringFromDbToCells() { long TruliaCount = DataProviders.DataProvider.Instance.GetPlacesCount(); //смотрим, сколько записей в таблице Console.WriteLine(TruliaCount); int range = 10000; for (int i = 0; i <= TruliaCount; i += range) { if ((i) % 1000 == 0) { Console.WriteLine("Now {0} cells", i); } List <AddressInfo> adresses; Console.WriteLine("Getting next part..."); if (i + range > TruliaCount) { adresses = GetJSONPlacesFromDb(i, TruliaCount); } else { adresses = GetJSONPlacesFromDb(i, i + range - 1); } ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = Convert.ToInt32(Resources.MaxDegreeOfParallelism); Parallel.ForEach(adresses, options, address => { //Console.WriteLine(adresses[j].PlaceLink); if (address.JSON != String.Empty && address.JSON != Constants.WebAttrsNames.NotFound) { GeocodeJsonObject o = ReadJsonToObject(address.JSON); RooftopResultPlace res = new RooftopResultPlace(o, address.ID); res.InsertToDb(); } else { Console.WriteLine("Запись с номером ID={0} не содержит JSON", address.ID); } }); } }