Excel VBA解析POST请求返回的JSON,提取barcodes写入单元格
Complete VBA Code to Parse JSON Barcodes and Write to Excel
Hey there! Let's finish up your VBA code to parse the JSON response and write the barcodes array starting from cell (1,1) (A1) in your Excel sheet. Here's a step-by-step solution:
Step 1: Required References
First, make sure you've enabled these references in the VBA Editor (go to Tools > References and check the boxes):
- Microsoft Scripting Runtime (for
Dictionaryobjects) - Microsoft Script Control 6.0 (for built-in JSON parsing; if this is unavailable in your Office version, use the VBA-JSON alternative below)
Step 2: Full Completed Code
Here's the complete code that builds on your existing variables and handles JSON parsing + writing to Excel:
Sub ExtractBarcodesFromJSON() Dim Json As Object Dim xmlhttp As New MSXML2.XMLHTTP60 Dim Parsed As Dictionary Dim barcodesArr As Variant Dim i As Integer ' --- (Optional) Add your POST request setup if not already written --- ' xmlhttp.Open "POST", "https://your-api-endpoint.com", False ' xmlhttp.setRequestHeader "Content-Type", "application/json" ' xmlhttp.Send "{""your-request-body-key"": ""value""}" ' Parse the JSON response using ScriptControl Set Json = CreateObject("ScriptControl") Json.Language = "JScript" ' Wrap response in parentheses to avoid JScript parsing issues Set Parsed = Json.Eval("(" & xmlhttp.responseText & ")") ' Extract the barcodes array from the nested "data" object barcodesArr = Parsed("data")("barcodes") ' Write each barcode to Excel starting from cell (1,1) (A1) For i = LBound(barcodesArr) To UBound(barcodesArr) ' JScript arrays use 0-based indexing, so i+1 maps to row 1, 2, etc. Cells(i + 1, 1).Value = barcodesArr(i) Next i ' Clean up objects to free memory Set xmlhttp = Nothing Set Json = Nothing Set Parsed = Nothing End Sub
Alternative: Using VBA-JSON Library (If ScriptControl is Disabled)
If ScriptControl is blocked in your Office environment (common in Office 365), use the popular VBA-JSON library:
- Download the
JsonConverter.basmodule (search for "VBA-JSON" and import it into your VBA project) - Use this modified code:
Sub ExtractBarcodesWithVBAJSON() Dim xmlhttp As New MSXML2.XMLHTTP60 Dim Parsed As Dictionary Dim barcodesArr As Variant Dim i As Integer ' --- (Optional) Add your POST request setup --- ' xmlhttp.Open "POST", "https://your-api-endpoint.com", False ' xmlhttp.setRequestHeader "Content-Type", "application/json" ' xmlhttp.Send "{""your-request-body-key"": ""value""}" ' Parse JSON with VBA-JSON Set Parsed = JsonConverter.ParseJson(xmlhttp.responseText) ' Extract barcodes array barcodesArr = Parsed("data")("barcodes") ' Write to Excel For i = LBound(barcodesArr) To UBound(barcodesArr) Cells(i + 1, 1).Value = barcodesArr(i) Next i ' Clean up Set xmlhttp = Nothing Set Parsed = Nothing End Sub
Key Notes
- The JSON response has nested structure:
status→data→barcodes, so we accessParsed("data")("barcodes")to get the array - The
barcodesarray is 0-indexed, so we usei + 1to start writing from row 1 (A1) - Always clean up objects with
Set [object] = Nothingto prevent memory leaks
内容的提问来源于stack exchange,提问作者Tarinder




