运行-时间错误“13”:显示来自 JSON 数组的数据时出现类型不匹配错误

Run-time error '13': Type Mismatch error on displaying data from JSON Array

我在显示来自 JSON 数组的数据时遇到 Run-time error '13': Type Mismatch 错误。
卡在 home3.Cells(vp, 8) = item41("nonVariableListPrice")。我正在通过研究 google 尝试许多不同的方式和方法,但仍然无法解决问题。我可以知道这是否是 JSON 转换器问题吗?
但是我能够从另一个 API 调用其他数组数据而没有任何错误...

请参考以下代码:

vp = 7


Lastrow2 = Noutput.Cells(Rows.Count, "F").End(xlUp).Row

For id = 2 To Lastrow2 'make a loop call for OpenBasket API
    If Cells(id, 6).Value <> "sn?pshot" Then
            
         Dim strUrl62 As String
        
        'For search quote service API-Step1
        strUrl62 = Cells(id, 6).Value
            
        'create a method for calling HTTP services
        Set hReq62 = CreateObject("MSXML2.XMLHTTP")
                
            With hReq62
                .Open "GET", strUrl62, blnAsync, False
                'Set the headers for bypassing the API service.
                .SetRequestHeader "Authorization", "xxxxxxxxxxxxxxxx" & authKey
                .Send
                While hReq62.ReadyState <> 4
                    DoEvents
                Wend
                
                'wrap the response in a JSON root tag "data" to count returned objects
                strResponse62 = hReq62.ResponseText

            End With
            
        Dim Json61 As Object
        Set Json61 = JsonConverter.ParseJson(strResponse62)
    
    For Each check41 In Json61
        If check41 = "nonVariableListPrice" Then
        
        
        For Each item41 In Json61
            **home3.Cells(vp, 8) = item41("nonVariableListPrice")**
            Debug.Print item41
    
    Next item41
        Else
        
        home3.Cells(vp, 8).Value = "NO DATA"
        
    End If
    
    
    Next check41
    vp = vp + 1

截图:
Error line of code
JSON Structure
Runtime Error '1004'
"Left" unexpected VS "Right"expected Output in Excel

示例JSON:


    {
        "associatedItems": [],
        "associatedTiedItems": [],
        "baseSkuClass": "XZ050",
        "baseSkuClassDescription": "CYBERSENSE SUBSCRIPTION",
        "baseSkuNumber": "210-AXQX",
        "brandId": "1263",
        "buildToStock": false,
        "catalogId": 18,
        "categories": [
            {
                "name": "CONFIG",
                "id": "root",
                "parentCategoryId": "-1",
                "description": "CONFIG"
            },
        ],
        "taxes": [],
        "type": "OrderCode",
        "weight": 0.0,
        "catalogSystemUri": "CFG",
        "allowChangeQuantity": true,
        "familyName": "CyberSense",
        "shipsWith": false,
        "nonVariableListPrice": 147876.912,
        "nonVariableCost": 0.0,
        "catalogCurrency": "USD",
        "nonVariableCurrency": "USD",
        "nonVariableHedgeRateUsed": 1.0,
        "isVariablePriced": true,
        "validationResult": {
            "isValid": true,
            "message": []
        },

item41 是字典 Json61 中的一个 key 而它本身不是一个 Dictionary 对象,所以你此处出现错误:

home3.Cells(vp, 8) = item41("nonVariableListPrice")

您不需要循环检查键的名称,因为字典有一个 Exists 方法:

Dim Json61 As Object
Set Json61 = JsonConverter.ParseJson(strResponse62)
    
If Json61.Exists("nonVariableListPrice") Then
    home3.Cells(vp, 8).Value = Json61("nonVariableListPrice")
Else
    home3.Cells(vp, 8).Value = "NO DATA"
End If

使用效用函数稍微清洁一些:

home3.Cells(vp, 8).Value = ValueOrDefault(Json61, "nonVariableListPrice", "NO DATA")

函数:

Function ValueOrDefault(dict as object, k, default)
    If dict.Exists(k) Then
        ValueOrDefault = dict(k)
    Else
        ValueOrDefault = default
    End If
End Function