Json API Abfrage - in eine Excel Tabelle schreiben

  • VBScript

    Json API Abfrage - in eine Excel Tabelle schreiben

    Hallo liebe Forums-Mitglieder,

    ich bin neu hier und habe direkt eine Frage, bzw Problemstellung.

    Ich möchte von der Binance Api den Preis für eine Paarung abfragen und in einer Excel Tabelle ausgeben. Die Paarung(Symbol) wird in Zelle A1 der Excel Datei eingetragen und Der passende Preis soll in Zelle B1 ausgegeben werden. Mei Script läuft, ohne einen Fehler zu melden, tut jedoch nicht dass, was ich von Ihm will. Mein Tel beginnt bei Zeile 323. Den Rest habe ich von: gist.github.com/douglascrp/26477fa0c523186b55705890a97b9821 und sollte so funktionieren.
    Es wäre schön, wenn sich das mal jemand anschauen könnte...

    Spoiler anzeigen

    Quellcode

    1. Class VbsJson
    2.     ' Author: Demon
    3.     ' Date: 2012/5/3
    4.     ' Website: http://demon.tw/my-work/vbs-json.html
    5.     Private Whitespace, NumberRegex, StringChunk
    6.     Private b, f, r, n, t
    7.     Private Sub Class_Initialize
    8.         Whitespace = " " & vbTab & vbCr & vbLf
    9.         b = ChrW(8)
    10.         f = vbFormFeed
    11.         r = vbCr
    12.         n = vbLf
    13.         t = vbTab
    14.         Set NumberRegex = New RegExp
    15.         NumberRegex.Pattern = "(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?"
    16.         NumberRegex.Global = False
    17.         NumberRegex.MultiLine = True
    18.         NumberRegex.IgnoreCase = True
    19.         Set StringChunk = New RegExp
    20.         StringChunk.Pattern = "([\s\S]*?)([""\\\x00-\x1f])"
    21.         StringChunk.Global = False
    22.         StringChunk.MultiLine = True
    23.         StringChunk.IgnoreCase = True
    24.     End Sub
    25.     ' Return a JSON string representation of a VBScript data structure
    26.     ' Supports the following objects and types
    27.     ' +-------------------+---------------+
    28.     ' | VBScript          | JSON          |
    29.     ' +===================+===============+
    30.     ' | Dictionary        | object        |
    31.     ' | Array             | array         |
    32.     ' | String            | string        |
    33.     ' | Number            | number        |
    34.     ' | True              | true          |
    35.     ' | False             | false         |
    36.     ' | Null              | null          |
    37.     ' +-------------------+---------------+
    38.     Public Function Encode(ByRef obj)
    39.         Dim buf, i, c, g
    40.         Set buf = CreateObject("Scripting.Dictionary")
    41.         Select Case VarType(obj)
    42.             Case vbNull
    43.                 buf.Add buf.Count, "null"
    44.             Case vbBoolean
    45.                 If obj Then
    46.                     buf.Add buf.Count, "true"
    47.                 Else
    48.                     buf.Add buf.Count, "false"
    49.                 End If
    50.             Case vbInteger, vbLong, vbSingle, vbDouble
    51.                 buf.Add buf.Count, obj
    52.             Case vbString
    53.                 buf.Add buf.Count, """"
    54.                 For i = 1 To Len(obj)
    55.                     c = Mid(obj, i, 1)
    56.                     Select Case c
    57.                         Case """" buf.Add buf.Count, "\"""
    58.                         Case "\"  buf.Add buf.Count, "\\"
    59.                         Case "/"  buf.Add buf.Count, "/"
    60.                         Case b    buf.Add buf.Count, "\b"
    61.                         Case f    buf.Add buf.Count, "\f"
    62.                         Case r    buf.Add buf.Count, "\r"
    63.                         Case n    buf.Add buf.Count, "\n"
    64.                         Case t    buf.Add buf.Count, "\t"
    65.                         Case Else
    66.                             If AscW(c) >= 0 And AscW(c) <= 31 Then
    67.                                 c = Right("0" & Hex(AscW(c)), 2)
    68.                                 buf.Add buf.Count, "\u00" & c
    69.                             Else
    70.                                 buf.Add buf.Count, c
    71.                             End If
    72.                     End Select
    73.                 Next
    74.                 buf.Add buf.Count, """"
    75.             Case vbArray + vbVariant
    76.                 g = True
    77.                 buf.Add buf.Count, "["
    78.                 For Each i In obj
    79.                     If g Then g = False Else buf.Add buf.Count, ","
    80.                     buf.Add buf.Count, Encode(i)
    81.                 Next
    82.                 buf.Add buf.Count, "]"
    83.             Case vbObject
    84.                 If TypeName(obj) = "Dictionary" Then
    85.                     g = True
    86.                     buf.Add buf.Count, "{"
    87.                     For Each i In obj
    88.                         If g Then g = False Else buf.Add buf.Count, ","
    89.                         buf.Add buf.Count, """" & i & """" & ":" & Encode(obj(i))
    90.                     Next
    91.                     buf.Add buf.Count, "}"
    92.                 Else
    93.                     Err.Raise 8732,,"None dictionary object"
    94.                 End If
    95.             Case Else
    96.                 buf.Add buf.Count, """" & CStr(obj) & """"
    97.         End Select
    98.         Encode = Join(buf.Items, "")
    99.     End Function
    100.     ' Return the VBScript representation of ``str(``
    101.     ' Performs the following translations in decoding
    102.     ' +---------------+-------------------+
    103.     ' | JSON          | VBScript          |
    104.     ' +===============+===================+
    105.     ' | object        | Dictionary        |
    106.     ' | array         | Array             |
    107.     ' | string        | String            |
    108.     ' | number        | Double            |
    109.     ' | true          | True              |
    110.     ' | false         | False             |
    111.     ' | null          | Null              |
    112.     ' +---------------+-------------------+
    113.     Public Function Decode(ByRef str)
    114.         Dim idx
    115.         idx = SkipWhitespace(str, 1)
    116.         If Mid(str, idx, 1) = "{" Then
    117.             Set Decode = ScanOnce(str, 1)
    118.         Else
    119.             Decode = ScanOnce(str, 1)
    120.         End If
    121.     End Function
    122.     Private Function ScanOnce(ByRef str, ByRef idx)
    123.         Dim c, ms
    124.         idx = SkipWhitespace(str, idx)
    125.         c = Mid(str, idx, 1)
    126.         If c = "{" Then
    127.             idx = idx + 1
    128.             Set ScanOnce = ParseObject(str, idx)
    129.             Exit Function
    130.         ElseIf c = "[" Then
    131.             idx = idx + 1
    132.             ScanOnce = ParseArray(str, idx)
    133.             Exit Function
    134.         ElseIf c = """" Then
    135.             idx = idx + 1
    136.             ScanOnce = ParseString(str, idx)
    137.             Exit Function
    138.         ElseIf c = "n" And StrComp("null", Mid(str, idx, 4)) = 0 Then
    139.             idx = idx + 4
    140.             ScanOnce = Null
    141.             Exit Function
    142.         ElseIf c = "t" And StrComp("true", Mid(str, idx, 4)) = 0 Then
    143.             idx = idx + 4
    144.             ScanOnce = True
    145.             Exit Function
    146.         ElseIf c = "f" And StrComp("false", Mid(str, idx, 5)) = 0 Then
    147.             idx = idx + 5
    148.             ScanOnce = False
    149.             Exit Function
    150.         End If
    151.         Set ms = NumberRegex.Execute(Mid(str, idx))
    152.         If ms.Count = 1 Then
    153.             idx = idx + ms(0).Length
    154.             ScanOnce = CDbl(ms(0))
    155.             Exit Function
    156.         End If
    157.         Err.Raise 8732,,"No JSON object could be ScanOnced"
    158.     End Function
    159.     Private Function ParseObject(ByRef str, ByRef idx)
    160.         Dim c, key, value
    161.         Set ParseObject = CreateObject("Scripting.Dictionary")
    162.         idx = SkipWhitespace(str, idx)
    163.         c = Mid(str, idx, 1)
    164.         If c = "}" Then
    165.             Exit Function
    166.         ElseIf c <> """" Then
    167.             Err.Raise 8732,,"Expecting property name"
    168.         End If
    169.         idx = idx + 1
    170.         Do
    171.             key = ParseString(str, idx)
    172.             idx = SkipWhitespace(str, idx)
    173.             If Mid(str, idx, 1) <> ":" Then
    174.                 Err.Raise 8732,,"Expecting : delimiter"
    175.             End If
    176.             idx = SkipWhitespace(str, idx + 1)
    177.             If Mid(str, idx, 1) = "{" Then
    178.                 Set value = ScanOnce(str, idx)
    179.             Else
    180.                 value = ScanOnce(str, idx)
    181.             End If
    182.             ParseObject.Add key, value
    183.             idx = SkipWhitespace(str, idx)
    184.             c = Mid(str, idx, 1)
    185.             If c = "}" Then
    186.                 Exit Do
    187.             ElseIf c <> "," Then
    188.                 Err.Raise 8732,,"Expecting , delimiter. Got " & c & " at " & idx
    189.             End If
    190.             idx = SkipWhitespace(str, idx + 1)
    191.             c = Mid(str, idx, 1)
    192.             If c <> """" Then
    193.                 Err.Raise 8732,,"Expecting property name"
    194.             End If
    195.             idx = idx + 1
    196.         Loop
    197.         idx = idx + 1
    198.     End Function
    199.     Private Function ParseArray(ByRef str, ByRef idx)
    200.         Dim c, values, value
    201.         Set values = CreateObject("Scripting.Dictionary")
    202.         idx = SkipWhitespace(str, idx)
    203.         c = Mid(str, idx, 1)
    204.         If c = "]" Then
    205.             idx = idx + 1
    206.             ParseArray = values.Items
    207.             Exit Function
    208.         End If
    209.         Do
    210.             idx = SkipWhitespace(str, idx)
    211.             If Mid(str, idx, 1) = "{" Then
    212.                 Set value = ScanOnce(str, idx)
    213.             Else
    214.                 value = ScanOnce(str, idx)
    215.             End If
    216.             values.Add values.Count, value
    217.             idx = SkipWhitespace(str, idx)
    218.             c = Mid(str, idx, 1)
    219.             If c = "]" Then
    220.                 Exit Do
    221.             ElseIf c <> "," Then
    222.                 Err.Raise 8732,,"Expecting , delimiter"
    223.             End If
    224.             idx = idx + 1
    225.         Loop
    226.         idx = idx + 1
    227.         ParseArray = values.Items
    228.     End Function
    229.     Private Function ParseString(ByRef str, ByRef idx)
    230.         Dim chunks, content, terminator, ms, esc, char
    231.         Set chunks = CreateObject("Scripting.Dictionary")
    232.         Do
    233.             Set ms = StringChunk.Execute(Mid(str, idx))
    234.             If ms.Count = 0 Then
    235.                 Err.Raise 8732,,"Unterminated string starting"
    236.             End If
    237.             content = ms(0).Submatches(0)
    238.             terminator = ms(0).Submatches(1)
    239.             If Len(content) > 0 Then
    240.                 chunks.Add chunks.Count, content
    241.             End If
    242.             idx = idx + ms(0).Length
    243.             If terminator = """" Then
    244.                 Exit Do
    245.             ElseIf terminator <> "\" Then
    246.                 Err.Raise 8732,,"Invalid control character"
    247.             End If
    248.             esc = Mid(str, idx, 1)
    249.             If esc <> "u" Then
    250.                 Select Case esc
    251.                     Case """" char = """"
    252.                     Case "\"  char = "\"
    253.                     Case "/"  char = "/"
    254.                     Case "b"  char = b
    255.                     Case "f"  char = f
    256.                     Case "n"  char = n
    257.                     Case "r"  char = r
    258.                     Case "t"  char = t
    259.                     Case Else Err.Raise 8732,,"Invalid escape"
    260.                 End Select
    261.                 idx = idx + 1
    262.             Else
    263.                 char = ChrW("&H" & Mid(str, idx + 1, 4))
    264.                 idx = idx + 5
    265.             End If
    266.             chunks.Add chunks.Count, char
    267.         Loop
    268.         ParseString = Join(chunks.Items, "")
    269.     End Function
    270.     Private Function SkipWhitespace(ByRef str, ByVal idx)
    271.         Do While idx <= Len(str) And _
    272.             InStr(Whitespace, Mid(str, idx, 1)) > 0
    273.             idx = idx + 1
    274.         Loop
    275.         SkipWhitespace = idx
    276.     End Function
    277. End Class
    278.      
    279.      
    280. '_____________________________________________________________
    281.  Dim fso, json, str, o, i
    282.      Set json = New VbsJson
    283.      Set fso = WScript. CreateObject ( "Scripting.Filesystemobject" )
    284.      Str = fso.OpenTextFile( "json.txt" ).ReadAll
    285.      Set o = json.Decode(str)
    286. Set http = CreateObject("MSXML2.XMLHTTP")
    287. http.Open "GET", "https://api.binance.com/api/v3/ticker/price", False
    288. On Error Resume Next
    289. http.Send
    290. 'On Error GoTo error
    291. Set JSON = ParseJson(http.ResponseText)
    292. Set ExcelApp = CreateObject("Excel.Application")
    293. Set MyBook = ExcelApp.Workbooks.Open("C:\Users\wir\Desktop\cb\VBscript\1.xlsm")
    294. Set MySheet = MyBook.Worksheets("Sheet1")
    295. For Each Item In JSON
    296.     If Item("symbol") = MySheet.Range("A1").Value And MySheet.Range("A1").Value <> "" Then
    297.     MySheet.Range("B1").Value = Item("price")
    298.     End If
    299.    
    300. Next


    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()