Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在excelvba中解析JSON数组_Arrays_Json_Excel_Http Post_Vba - Fatal编程技术网

Arrays 在excelvba中解析JSON数组

Arrays 在excelvba中解析JSON数组,arrays,json,excel,http-post,vba,Arrays,Json,Excel,Http Post,Vba,我做了一个post URL,得到了JSON格式的respondtext。 我想从单元格(1,1)开始获取“条形码” 这就是JSON响应的含义: "{ ""status"": ""success"", ""message"": ""5 New Barcode(s) Generated for batch 5924592"", ""data"": { ""batch"": ""5924592"", ""barcodes"": [

我做了一个post URL,得到了JSON格式的respondtext。 我想从单元格(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开发人员


谢谢

您需要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()都会让它看起来很混乱,但我不喜欢在带引号的字符串中加引号。

我在运行脚本时遇到了一个错误,“需要对象”:(我同意双引号。在这种情况下,我倾向于使用常量:
const dq=Chr(34)&Chr(34)
。可能不是最好的编程策略,但有时我不知道最佳实践。这只是一个演示,它的有用性取决于responseText的实际外观。最好将字符串介于
[
]
之间,然后从那里正则表达式出条形码。