无法找出将长参数分成多行的正确方法

Can't figure out the right way to break a long parameter to multiple lines

我创建了一个脚本来从网站获取 json 响应。为了获得响应,我必须发出 post 个带有适当参数的 http 请求。脚本运行良好。

我在脚本中使用的 payload 相当长。它本来可以更长。

Now, my question is, how can I break such long line to multiple lines?

这是我试过的方法:

Sub GetJsonResponse()
    Const URL = "https://api.pcexpress.ca/product-facade/v3/products/category/listing"
    Dim payload$

    payload = "{""pagination"":{""from"":2,""size"":48},""banner"":""loblaw"",""cartId"":""702da51e-a7ab-4f54-be5e-5bf38bd6d7a2"",""lang"":""en"",""date"":""09062021"",""storeId"":""1032"",""pcId"":null,""pickupType"":""STORE"",""enableSeldonIntegration"":true,""features"":[""loyaltyServiceIntegration"",""sunnyValeServiceIntegration""],""inventoryInfoRequired"":true,""sort"":{""topSeller"":""desc""},""categoryId"":""27985""}"
    
    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", URL, False
        .setRequestHeader "content-type", "application/json;charset=UTF-8"
        .setRequestHeader "x-apikey", "1im1hL52q9xvta16GlSdYDsTsG0dmyhF"
        .send (payload)
        Debug.Print .responseText
    End With
End Sub

使用 & 串联连接更小的部分。我会亲自检查 json 结构,然后决定逻辑中断(在合理范围内),然后转移到文本编辑器并使用正则表达式/查找和替换生成新字符串以根据您选择的换行符连接。

下面您会看到大多数行的开头为 payload = payload & ",结尾为 ",在 , 指示的中断之后。

当然,也将内部"替换为""

Option Explicit

Sub GetJsonResponse()
    Const URL = "https://api.pcexpress.ca/product-facade/v3/products/category/listing"
    Dim payload$

    payload = "{""pagination"": {""from"": 2,""size"": 48},"
    payload = payload & """banner"": ""loblaw"","
    payload = payload & """cartId"": ""702da51e-a7ab-4f54-be5e-5bf38bd6d7a2"","
    payload = payload & """lang"": ""en"","
    payload = payload & """date"": ""09062021"","
    payload = payload & """storeId"": ""1032"","
    payload = payload & """pcId"": null,"
    payload = payload & """pickupType"": ""STORE"","
    payload = payload & """enableSeldonIntegration"": true,"
    payload = payload & """features"": [""loyaltyServiceIntegration"", ""sunnyValeServiceIntegration""],"
    payload = payload & """inventoryInfoRequired"": true,"
    payload = payload & """sort"": {""topSeller"": ""desc""},"
    payload = payload & """categoryId"": ""27985""}"

    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", URL, False
        .setRequestHeader "content-type", "application/json;charset=UTF-8"
        .setRequestHeader "x-apikey", "1im1hL52q9xvta16GlSdYDsTsG0dmyhF"
        .send payload
        Debug.Print .responseText
    End With
End Sub

这符合我重新安排的方式:

为此:


正如您在评论中指出的那样,您绝对可以将字符串拆分成多个部分并使用 line continuation character _.

继续该行

使用此处的 Windows 剪贴板 API 功能: https://docs.microsoft.com/en-us/office/vba/access/concepts/windows-api/send-information-to-the-clipboard

您可以将文本复制到剪贴板并执行以下操作:

Sub ClipboardTextToVbaString()
    Dim s As String, arr, e, rv As String, i As Long, n As Long
    s = GetClipboard()  'read text from clipboard
    If Len(s) = 0 Then Exit Sub
    arr = Split(s, vbCrLf)
    rv = "s = "
    For i = LBound(arr) To UBound(arr)
        e = Replace(arr(i), """", """""")
        rv = rv & """" & e & """ "
        If i < UBound(arr) Then
            If n < 20 Then
                rv = rv & " & vbCRLf &  _" & vbCrLf
                n = n + 1
            Else
                rv = rv & " & vbCRLf" & vbCrLf & "s = s & "
                n = 0
            End If
        End If
    Next i
    'Debug.Print rv
    SetClipboard rv  'set the modified text back into the clipboard for pasting
End Sub

没有经过彻底的测试,但你明白了:你的 personal.xlsb 文件的东西...... 请注意,这更多是为了将多行文本格式化为 VB 兼容格式 - 并不是真正用于拆分长单行,我猜这是您的原始格式。