0

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

payload我在脚本中使用的很长。它可能会更长。

现在,我的问题是,我怎样才能将这么长的一行分成多行?

这就是我尝试过的方式:

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
4

2 回答 2

1

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

下面你会看到大多数行在payload = payload & "开头和"结尾都有,在,.

当然,也可以将 inner 替换""".

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

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

对此:

在此处输入图像描述


正如您在评论中指出的那样,您绝对可以将字符串分成几部分,并使用续行 _继续该行。

于 2021-06-11T18:21:26.610 回答
1

从这里使用 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 兼容的格式 - 而不是真正用于分解长单行,我猜测是您的原始形式。

于 2021-06-12T00:22:08.963 回答