반응형

* VBNET 한국 수출입 은행 공공데이터를 이용한 환율 정보 가져 오기 예제...

 

Main

 

- 사용한 컨트롤 : DateTimepicker 1개, Button 1개, Listview 1개

 

전체 소스 코드

Form1.vb



Imports System.Net.Json
Imports System.IO
Imports System.Net

Public Class Form1

    Public Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
    Public Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        SetRoundControl(listView1, 15)

        MyBase.OnLoad(e)
    End Sub
    Private Sub Form1_ResizeEnd(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeEnd
        SetRoundControl(listView1, 15)
    End Sub

    Private Sub SetRoundControl(ByVal ct As Control, ByVal iValue As Integer)
        Dim ip As IntPtr = CreateRoundRectRgn(0, 0, ct.Width, ct.Height, iValue, iValue)
        Dim i As Integer = SetWindowRgn(ct.Handle, ip, True)
    End Sub

    Private Function StringToJson(ByVal strTmp As String, ByRef strErr As String) As Boolean

        If strTmp.Length < 3 Then
            strErr = "영업일이 아닙니다. 확인 후 이용해 주세요."
            Return False
        End If

        listView1.Items.Clear()

        Dim jtr As JsonTextParser = New JsonTextParser()
        'string 문자열 -> json 으로 변환
        Dim jo As JsonObject = jtr.Parse(strTmp)

        Dim jac As JsonArrayCollection = CType(jo, JsonArrayCollection)
        Dim iRow As Integer = 0

        Dim joc As JsonObjectCollection

        For Each joc In jac
            iRow += 1
            'Display
            JsonDataSet(iRow, joc)
        Next

        Return True

    End Function


    Private Sub JsonDataSet(ByVal iRow As Integer, ByVal joc As JsonObjectCollection)
        'success 
        If joc("result").ToString().Contains("1") Then
            Dim lvi As ListViewItem = New ListViewItem()
            lvi.Text = iRow.ToString()
            'ex) cur_nm : "미국 달러" => split(:)[1] "미국 달러" => Replace " 공백 치환으로 미국 달러 

            lvi.SubItems.Add(joc("cur_unit").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("cur_nm").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("ttb").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("tts").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("deal_bas_r").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("bkpr").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("yy_efee_r").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("kftc_deal_bas_r").ToString().Split(":")(1).Replace("""", " ").Trim())
            lvi.SubItems.Add(joc("kftc_bkpr").ToString().Split(":")(1).Replace("""", " ").Trim())

            listView1.Items.Add(lvi)
        End If

    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        'AP01 : 환율, AP02 : 대출금리, AP03 : 국제금리
        'ex) 2015-01-01, 20150101, (DEFAULT)현재일
        '(요청) https://www.koreaexim.go.kr/site/program/financial/exchangeJSON?authkey=AUTHKEY1234567890&searchdate=20180102&data=AP01
        '(응답) [{"result":1,"cur_unit":"AED","ttb":"288.78","tts":"294.61","deal_bas_r":"291.7","bkpr":"291"
        '        ,"yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"291","kftc_deal_bas_r":"291.7" 
        '        ,"cur_nm":"아랍에미리트 디르함"},"result":1,"cur_unit":"ATS","ttb":"0","tts":"0"
        '        ,"deal_bas_r":"93.52","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0"
        '        ,"kftc_deal_bas_r":"93.52","cur_nm":"오스트리아 실링"},{"result":1,"cur_unit":"AUD","ttb":"827.91","tts":"844.64","deal_bas_r":"836.28","bkpr":"836","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"836","kftc_deal_bas_r":"836.28","cur_nm":"호주 달러"},{"result":1,"cur_unit":"BEF","ttb":"0","tts":"0","deal_bas_r":"31.9","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"31.9","cur_nm":"벨기에 프랑"},{"result":1,"cur_unit":"BHD","ttb":"2,811.48","tts":"2,868.27","deal_bas_r":"2,839.88","bkpr":"2,839","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"2,839","kftc_deal_bas_r":"2,839.88","cur_nm":"바레인 디나르"},{"result":1,"cur_unit":"CAD","ttb":"844.72","tts":"861.79","deal_bas_r":"853.26","bkpr":"853","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"853","kftc_deal_bas_r":"853.26","cur_nm":"캐나다 달러"},{"result":1,"cur_unit":"CHF","ttb":"1,088.44","tts":"1,110.43","deal_bas_r":"1,099.44","bkpr":"1,099","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"1,099","kftc_deal_bas_r":"1,099.44","cur_nm":"스위스 프랑"},{"result":1,"cur_unit":"CNH","ttb":"162.01","tts":"165.28","deal_bas_r":"163.65","bkpr":"163","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"163","kftc_deal_bas_r":"163.65","cur_nm":"위안화"},{"result":1,"cur_unit":"DEM","ttb":"0","tts":"0","deal_bas_r":"657.98","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"657.98","cur_nm":"독일 마르크"},{"result":1,"cur_unit":"DKK","ttb":"171.14","tts":"174.59","deal_bas_r":"172.87","bkpr":"172","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"172","kftc_deal_bas_r":"172.87","cur_nm":"덴마아크 크로네"},{"result":1,"cur_unit":"ESP(100)","ttb":"0","tts":"0","deal_bas_r":"773","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"773","cur_nm":"스페인 페세타"},{"result":1,"cur_unit":"EUR","ttb":"1,274.04","tts":"1,299.77","deal_bas_r":"1,286.91","bkpr":"1,286","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"1,286","kftc_deal_bas_r":"1,286.91","cur_nm":"유로"},{"result":1,"cur_unit":"FIM","ttb":"0","tts":"0","deal_bas_r":"216.44","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"216.44","cur_nm":"핀란드 마르카"},{"result":1,"cur_unit":"FRF","ttb":"0","tts":"0","deal_bas_r":"196.18","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"196.18","cur_nm":"프랑스 프랑"},{"result":1,"cur_unit":"GBP","ttb":"1,432.82","tts":"1,461.77","deal_bas_r":"1,447.3","bkpr":"1,447","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"1,447","kftc_deal_bas_r":"1,447.3","cur_nm":"영국 파운드"},{"result":1,"cur_unit":"HKD","ttb":"135.75","tts":"138.5","deal_bas_r":"137.13","bkpr":"137","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"137","kftc_deal_bas_r":"137.13","cur_nm":"홍콩 달러"},{"result":1,"cur_unit":"IDR(100)","ttb":"7.82","tts":"7.97","deal_bas_r":"7.9","bkpr":"7","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"7","kftc_deal_bas_r":"7.9","cur_nm":"인도네시아 루피아"},{"result":1,"cur_unit":"ITL(100)","ttb":"0","tts":"0","deal_bas_r":"66.46","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"66.46","cur_nm":"이태리 리라"},{"result":1,"cur_unit":"JPY(100)","ttb":"941.53","tts":"960.56","deal_bas_r":"951.05","bkpr":"951","yy_efee_r":"0.96833","ten_dd_efee_r":"0.0242","kftc_bkpr":"951","kftc_deal_bas_r":"951.05","cur_nm":"일본 옌"},{"result":1,"cur_unit":"KRW","ttb":"0","tts":"0","deal_bas_r":"1","bkpr":"1","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"1","kftc_deal_bas_r":"1","cur_nm":"한국 원"},{"result":1,"cur_unit":"KWD","ttb":"3,509.87","tts":"3,580.78","deal_bas_r":"3,545.33","bkpr":"3,545","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"3,545","kftc_deal_bas_r":"3,545.33","cur_nm":"쿠웨이트 디나르"},{"result":1,"cur_unit":"MYR","ttb":"262.09","tts":"267.38","deal_bas_r":"264.74","bkpr":"264","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"264","kftc_deal_bas_r":"264.74","cur_nm":"말레이지아 링기트"},{"result":1,"cur_unit":"NLG","ttb":"0","tts":"0","deal_bas_r":"583.97","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"583.97","cur_nm":"네델란드 길더"},{"result":1,"cur_unit":"NOK","ttb":"129.44","tts":"132.05","deal_bas_r":"130.75","bkpr":"130","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"130","kftc_deal_bas_r":"130.75","cur_nm":"노르웨이 크로네"},{"result":1,"cur_unit":"NZD","ttb":"752.76","tts":"767.97","deal_bas_r":"760.37","bkpr":"760","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"760","kftc_deal_bas_r":"760.37","cur_nm":"뉴질랜드 달러"},{"result":1,"cur_unit":"SAR","ttb":"282.84","tts":"288.55","deal_bas_r":"285.7","bkpr":"285","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"285","kftc_deal_bas_r":"285.7","cur_nm":"사우디 리얄"},{"result":1,"cur_unit":"SEK","ttb":"129.3","tts":"131.91","deal_bas_r":"130.61","bkpr":"130","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"130","kftc_deal_bas_r":"130.61","cur_nm":"스웨덴 크로나"},{"result":1,"cur_unit":"SGD","ttb":"793.06","tts":"809.09","deal_bas_r":"801.08","bkpr":"801","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"801","kftc_deal_bas_r":"801.08","cur_nm":"싱가포르 달러"},{"result":1,"cur_unit":"THB","ttb":"32.57","tts":"33.22","deal_bas_r":"32.9","bkpr":"32","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"32","kftc_deal_bas_r":"32.9","cur_nm":"태국 바트"},{"result":1,"cur_unit":"USD","ttb":"1,056.23","tts":"1,077.56","deal_bas_r":"1,066.9","bkpr":"1,066","yy_efee_r":"2.69465","ten_dd_efee_r":"0.07485","kftc_bkpr":"1,071","kftc_deal_bas_r":"1,071.4","cur_nm":"미국 달러"},{"result":1,"cur_unit":"XOF","ttb":"0","tts":"0","deal_bas_r":"1.9618","bkpr":"0","yy_efee_r":"0","ten_dd_efee_r":"0","kftc_bkpr":"0","kftc_deal_bas_r":"1.9618","cur_nm":"씨에프에이 프랑(비씨에이오)"}]

        Dim strURL As String = "https://www.koreaexim.go.kr/site/program/financial/exchangeJSON?authkey=" + _
                               "DhvTGvqzZYg58GZMbo0C2eBhOHnLtj10&searchdate=" + _
                               dtp.Value.ToString("yyyyMMdd") + "&data=" + _
                               "AP01"
        Dim hwr As HttpWebRequest = WebRequest.Create(strURL)
        hwr.ContentType = "application/json"

        Using hwrResult As HttpWebResponse = CType(hwr.GetResponse(), HttpWebResponse)
            Dim sr As Stream = hwrResult.GetResponseStream()

            Using srd As StreamReader = New StreamReader(sr)
                Dim strResult As String = srd.ReadToEnd()
                Dim strErr As String = ""

                If (Not StringToJson(strResult, strErr)) Then
                    MessageBox.Show(strErr, "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                End If

            End Using
            sr.Close()
            hwrResult.Close()
        End Using


    End Sub

    
End Class

 

아래의 그림과 같이 Button Click Event 를 살펴 보면

strURL 부분의 지워진 부분은 각자 한국 수출입 은행에서 받은 인증키를 입력 하면 됩니다. 

https://kdsoft-zeros.tistory.com/247

 

[C#] [공공데이터] 한국 수출입 은행 Open API 사용하기전 가입 신청...

https://www.koreaexim.go.kr/site/main/index001 한국수출입은행 www.koreaexim.go.kr 네이버 에서 한국 수출입 은행 으로 검색 하시면 위 화면과 같이 홈페이지에 접속 할 수 있습니다. 홈페이지에 접속 후 정..

kdsoft-zeros.tistory.com


인증키를 입력 후 한국수출입은행 응답을 보면 주석에서 보는거와 같이 Json 형태로 응답이 오게

됩니다.

 

 

https://kdsoft-zeros.tistory.com/98

 

[VBNET] Json File Write & Read 예제...

* VBNET Json 파일 읽고 쓰기 예제... (System.Net.Json.dll) 파일 참조 위 첨부된 파일을 다운 받아 dll 참조 추가를 해줍니다. 오른쪽에 솔루션 탐색기가 나타나지 않는다면, 상단 메뉴 (보기) -> (솔루션탐

kdsoft-zeros.tistory.com

 

 

* 예제 결과

 

Result

 

반응형

+ Recent posts