위 사이트에서 배포하는 VBA모듈을 적용하면Json 파일 을 VBA 변수 Dictionary로 저장할 수 있다.


objStream = CreateObject ( "ADODB.Stream") 
objStream.Charset = "utf-8"
objStream.Open 
objStream.LoadFromFile ("파일위치") 
jsontext = objStream.ReadText ()

위 형태로 ADODB로 불러온 뒤 JsonConverter 에 넣어야 적용이 가능하다.


예제

{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
          "lat": "-37.3159",
          "lng": "81.1496"
      }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
  }
},
위와 같은 json 파일을 JsonConverter를 이용해 VBA에 담았을 때 해당 값을 호출하는 방법은 item("key1")("key2")의 형식으로 호출이 가능하다.

debug.print(item("address")("street")) 를 하는 경우 "Kulas Light"가 출력된다.

    Dim JSON As Object
    Set JSON = ParseJson(jsontext)
    On Error Resume Next
      Debug.Print JSON("address")("city") < 에러

    For Each Item In JSON
        Debug.Print Item("address")("city") <정상출력
    Next Item

    On Error GoTo 0
    For Each Item In JSON
        Debug.Print Item("address")("geo")("lng") <정상
    Next Item
위와 같은 형식으로 사용해야 함.



'#창고 > 스크립트' 카테고리의 다른 글

[VBA] 자주 사용하는 기본 스크립트  (0) 2018.11.18

+ Recent posts