我做了一个帖子网址,我得到了JSON格式的responsetext。 我想从单元格(1,1)开始“条形码” 这就是JSON的回应:
"{
""status"": ""success"",
""message"": ""5 New Barcode(s) Generated for batch 5924592"",
""data"": {
""batch"": ""5924592"",
""barcodes"": [
""MN8HY6"",
""5BZZ9K"",
""9R6QKR"",
""869P8Z"",
""XK2UXZ""
]
}
}"
以下是我迄今为止在VBA excel上所写的内容
Dim Json As Object
Dim xmlhttp As New MSXML2.XMLHTTP60
Dim Parsed As Dictionary
Dim Item As Dictionary
'Post
xmlhttp.Open "POST", "url", False
'setting auth headers
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Authorization", "token"
Dim Bqty As Variant
Dim bID As Variant
Bqty = Sheets("sheet1").Range("f1").Value
bID = Sheets("sheet1").Range("f4").Value
xmlhttp.send "action=" & "createBarcodes" & _
Chr(38) & "barcodes=" & Bqty & _
Chr(38) & "batch=" & bID
JsonString = xmlhttp.responseText
Set Json = JsonConverter.ParseJson(xmlhttp.responseText)
Set Parsed = JsonConverter.ParseJson(xmlhttp.responseText)
Dim i As Integer
i = 1
For Each Item In Parsed("data")
Sheets("Batch ID").Cells(i, 1).Value = Item("barcodes")
i = i + 1
Next
如果有人可以提供帮助,请我花一整天的时间来解决这个问题。我不是专业的VBA开发人员。
谢谢
答案 0 :(得分:1)
您需要JSON库吗?
Dim i As Long, strresp As String, bcodes As Variant
strresp = xmlhttp.responseText
strresp = Mid(strresp, InStr(1, strresp, Chr(91) & Chr(32) & Chr(34) & Chr(34)) + 4)
strresp = Left(strresp, InStrRev(strresp, Chr(34) & Chr(34) & Chr(32) & Chr(93)) - 1)
bcodes = Split(strresp, Chr(34) & Chr(34) & Chr(44) & Chr(32) & Chr(34) & Chr(34))
For i = LBound(bcodes) To UBound(bcodes)
Debug.Print bcodes(i)
Next i
Worksheets("Batch ID").Cells(1, 1).Resize(UBound(bcodes) + 1, 1) = _
Application.Transpose(bcodes)
当然所有的Chr()都让它看起来很乱,但我讨厌在引用的字符串中加倍引号。