■ 카카오 API를 사용해 키워드 장소를 검색하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System; using System.IO; using System.Net; using System.Text; using System.Web.Script.Serialization; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.Write("검색 키워드 : "); string keyword = Console.ReadLine(); string url = $"https://dapi.kakao.com/v2/local/search/keyword.json?query={keyword}"; string restAPIKey = "[REST API 키를 설정해 주시기 바랍니다]"; string header = $"KakaoAK {restAPIKey}"; WebRequest request = WebRequest.Create(url); request.Headers.Add("Authorization", header); WebResponse response = request.GetResponse(); string json = null; using(Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); json = reader.ReadToEnd(); } if(string.IsNullOrEmpty(json)) { return; } JavaScriptSerializer serializer = new JavaScriptSerializer(); dynamic responseData = serializer.Deserialize<dynamic>(json); dynamic documents = responseData["documents"]; int length = ((object[])documents).Length; for(int i = 0; i < length; i++) { string placeName = documents[i]["place_name"]; string x = documents[i]["x"]; string y = documents[i]["y"]; Console.WriteLine($"{placeName} : {x}, {y}"); } } #endregion } } |