-
Notifications
You must be signed in to change notification settings - Fork 6
/
JsonTest.bas
94 lines (69 loc) · 2.95 KB
/
JsonTest.bas
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Attribute VB_Name = "JsonTest"
Option Compare Text
Option Explicit
'
' Functions for simple testing and listing of retrieved
' data from a Json service.
' Call a Json service and return result as a collection and a messagebox.
'
Public Sub TestJsonService()
Dim DataCollection As Collection
Dim ServiceUrl As String
Dim ResponseText As String
Dim UserAgent As String
Const Username As String = "demo"
Const App_id As String = "b492b663ae3e458d9f0b042e8edb8c63"
' Register at http://www.geonames.org/login
'ServiceUrl = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=" & Username
' Register at https://openexchangerates.org/signup/free
'ServiceUrl = "http://openexchangerates.org/api/latest.json?app_id=" & App_id
' ServiceUrl = "http://cvrapi.dk/api?name=lagkagehuset&country=dk&format=json&version=0"
ServiceUrl = "http://cvrapi.dk/api?vat=12002696&country=dk&format=json&version=4"
UserAgent = "Example Org. - TestApp"
If RetrieveDataResponse(ServiceUrl, ResponseText, UserAgent) = True Then
Set DataCollection = CollectJson(ResponseText)
MsgBox "Retrieved" & Str(DataCollection.Count) & " root member(s)", vbInformation + vbOKOnly, "Web Service Success"
ElseIf ResponseText <> "" Then
MsgBox ResponseText, vbCritical + vbOKOnly, "Web Service Error"
End If
Call ListFieldNames(DataCollection)
Set DataCollection = Nothing
End Sub
' Analyze a manually entered Json string.
'
Public Sub TestJsonResponseText( _
ByVal ResponseText As String)
Dim DataCollection As Collection
ResponseText = InputBox("Json")
If ResponseText <> "" Then
Set DataCollection = CollectJson(ResponseText)
MsgBox "Retrieved" & Str(DataCollection.Count) & " root member(s)", vbInformation + vbOKOnly, "Web Service Success"
End If
Call ListFieldNames(DataCollection)
Set DataCollection = Nothing
End Sub
' List field names of a collection of arrays.
'
Public Sub ListFieldNames( _
ByVal DataCollection As Collection, _
Optional Indent As String)
On Error GoTo Err_ListFieldNames
Dim Index As Long
Dim MemberName As String
For Index = 1 To DataCollection.Count
MemberName = Space(16)
LSet MemberName = DataCollection(Index)(CollectionItem.Name)
Debug.Print Indent & MemberName, ;
If VarType(DataCollection(Index)(CollectionItem.Data)) = vbObject Then
Debug.Print
Call ListFieldNames(DataCollection(Index)(CollectionItem.Data), Indent & vbTab)
Else
Debug.Print Trim(DataCollection(Index)(CollectionItem.Data))
End If
Next
Exit_ListFieldNames:
Exit Sub
Err_ListFieldNames:
Debug.Print "Error" & Str(Err.Number) & ": " & Err.Description
Resume Exit_ListFieldNames
End Sub