private void AddOtherData(PopulationData populationData, UInt32 geocode, DopaStatisticsType dataType) { var jsonData = GetDataFromDopa(geocode, dataType); if (populationData.register == null) { populationData.register = new RegisterData(); } PopulationChangeEntry otherData = ParseAdditionalJson(jsonData); otherData.type = _populationChangeType[dataType]; populationData.register.change.Add(otherData); }
/// <summary> /// Download the population data from DOPA. /// </summary> /// <param name="geocode">Geocode of the entity.</param> /// <param name="statisticsType">Statistics type.</param> /// <returns>JSON object returned from website.</returns> private JsonObject GetDataFromDopa(UInt32 geocode, DopaStatisticsType statisticsType) { JsonObject obj = null; String url; if (geocode < 100) { url = String.Format(CultureInfo.InvariantCulture, _urlDataChangwat, _yearShort, geocode, (Int32)statisticsType); } else if (geocode < 10000) { url = String.Format(CultureInfo.InvariantCulture, _urlDataAmphoe, _yearShort, geocode, (Int32)statisticsType); } else { url = String.Format(CultureInfo.InvariantCulture, _urlDataTambon, _yearShort, geocode, (Int32)statisticsType); } Int32 errorCount = 0; while (obj == null) { try { WebClient webClient = new System.Net.WebClient(); Stream inputStream = webClient.OpenRead(url); String response = StreamToString(inputStream); JsonValue result = JsonValue.readFrom(response); if (!result.isObject()) { return(null); } obj = result.asObject(); } catch { errorCount++; } if (errorCount > maxRetry) { throw new InvalidDataException(String.Format("Failed to get parseable json data for {0}", geocode)); } } return(obj); }