The_Immortal
The_Immortal

Reputation: 125

Retrieve information from JSON string thru VBScript

I'm trying to get some information from that JSON-string.

I found an example how to do and and tried to adapt if for my task:

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

'  A JSON array must be loaded using JsonArray:
set jsonArray = CreateObject("Chilkat_9_5_0.JsonArray")

success = jsonArray.Load("https://api.crypto-bridge.org/api/v1/ticker")

If (success <> 1) Then
    outFile.WriteLine(jsonArray.LastErrorText)  '!!! "Unable to get array at index 0."
    WScript.Quit
End If

'  Examine the values:
i = 0
Do While i < jsonArray.Size
    ' jsonObj is a Chilkat_9_5_0.JsonObject
    Set jsonObj = jsonArray.ObjectAt(i)
    outFile.WriteLine(i & ": " & jsonObj.StringOf("last"))

    i = i + 1
Loop

outFile.Close

And all I get in output.txt is

 ChilkatLog:
  Load:
    ChilkatVersion: 9.5.0.71
    Unable to get array at index 0.
  --Load
--ChilkatLog

Could you help me please to understand what am I doing wrong? Thank you!

Upvotes: 1

Views: 4553

Answers (1)

user692942
user692942

Reputation: 16682

If in doubt read the documentation

Load(jsonArray As String) As Long

Introduced in version 9.5.0.64

Loads a JSON array from a string. A JSON array must begin with a "[" and end with a "]".

Note: The Load method causes the JsonArray to detach and become it's own JSON document. It should only be called on new instances of the JsonArray. See the example below.

Returns 1 for success, 0 for failure.

The Load() method expects a JSON Array string not a URL to a JSON Array. First use XHR to download the JSON into a string then call jsonArray.Load(download_json_string).

Simple example of an XHR would be;

Dim url, req, json
url = "https://api.crypto-bridge.org/api/v1/ticker"

Set req = CreateObject("Msxml2.XMLHttp.6.0")
Call req.Open("GET", url, False)
Call req.Send()

If req.Status = 200 Then
  json = req.responseText
End If

Useful Links

Upvotes: 1

Related Questions