다음 API 반환 구조 클래스
#region " [ 다음 주소검색 ] " public class DaumAddressSearchResult { public Channel channel { get; set; } } public class Channel { public string totalCount { get; set; } public string link { get; set; } public string result { get; set; } public string generator { get; set; } public string pageCount { get; set; } public string lastBuildDate { get; set; } public Item[] item { get; set; } public string title { get; set; } public string description { get; set; } } public class Item { public string mountain { get; set; } public string mainAddress { get; set; } public string point_wx { get; set; } public string point_wy { get; set; } public string isNewAddress { get; set; } public string buildingAddress { get; set; } public string title { get; set; } public string placeName { get; set; } public string zipcode { get; set; } public string newAddress { get; set; } public string localName_2 { get; set; } public string localName_3 { get; set; } public string localName_1 { get; set; } public float lat { get; set; } public float point_x { get; set; } public float lng { get; set; } public string zone_no { get; set; } public string subAddress { get; set; } public string id { get; set; } public float point_y { get; set; } } #endregion결과 반환 클래스
#region " [ 결과 반환 클래스 ] " public class ResultClass { public int Code { get; set; } public string Message { get; set; } public string Data { get; set; } public string Etc { get; set; } } #endregion
실행 함수
//[HttpPost] //[ValidateAntiForgeryToken] public ResultClass DaumMapAddressSearch(string address) { string actualJson = string.Empty; string json = string.Empty; string serviceUrl = "https://apis.daum.net/local/geo/addr2coord?apikey={0}&output=json&q={1}"; string serviceKey = "{{API KEY}}"; string searchedAddress = string.Empty; int responseItemCount = 0; serviceUrl = string.Format(serviceUrl, serviceKey, address); try { _result.Code = -1; _result.Message = "EXCEPTION"; _result.Data = ""; WebRequest request = WebRequest.Create(serviceUrl); request.Credentials = CredentialCache.DefaultCredentials; WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); var responseResult = JsonConvert.DeserializeObject<DaumAddressSearchResult>(responseFromServer); if (responseResult != null) { responseItemCount = Convert.ToInt32(responseResult.channel.totalCount); if (responseItemCount > 0) { _result.Code = 0; _result.Message = "OK"; _result.Data = responseFromServer; } else { _result.Code = -2; _result.Message = "NO_DATA"; } } else { _result.Code = -2; _result.Message = "NO_DATA"; } } catch (Exception ex) { _result.Code = -1; _result.Message = "EXCEPTION"; _result.Data = ex.ToString(); } return _result; }
'Web_Application > C#' 카테고리의 다른 글
[c#][mssql] Spatial Data type을 취급하기 위한 필수 패키지 (0) | 2017.07.12 |
---|---|
Convert string format to Datetime (0) | 2017.06.27 |
[c#] 해당 경로에 파일 복사하기 (0) | 2017.05.12 |
[c#] 경과 시간을 표시할 때 (0) | 2017.05.12 |
[c#] StreamWriter 로 텍스트 파일 생성/쓰기 (0) | 2017.05.11 |