Excel JSON VBA 解析 - 判断数组是否为空

Excel JSON VBA Parsing - Determining if an array is empty

我正在尝试解析 JSON 响应。我无法使用 VBA-JSON 库。我需要检查嵌套数组是否为空或 null。我不断收到此错误:

示例JSON:

{
"gardenAssets": [],
"gardenAssetsAlertCount": 0,
"gardenAssetsCount": 0,
"gardenAssetsErrorCount": 0,
"locationsSummaries": [
    {
        "locations": [
            {
                "auditOrder": "102",
                "code": "POT 102",
                "name": "POT 102",
                "type": "ProcessingLocation",
                "gardenAssets": [
                    {
                        "annotation": "Pallets",
                        "broker": {
                            "code": "TMTO",
                            "isOwner": null,
                            "name": null
                        },
                        "datetimeOfArrivalIngarden": 1622754283.937,
                        "id": "crusaf",
                        "isSealable": true,
                        "load": null,
                        "mastergardenCode": null,
                        "name": null,
                        "owner": {
                            "code": "SUN",
                            "isOwner": null,
                            "name": null
                        }
                    }
                ]
            },
            {
                "auditOrder": "103",
                "code": "POT 103",
                "description": "POT 103",
                "id": "110746",
                "name": "POT 103",
                "type": "ProcessingLocation",
                "gardenAssets": []
            },
            {
                "auditOrder": "104",
                "code": "POT 104",
                "name": "POT 104",
                "gardenAssets": [
                    {
                        "annotation": "Soil",
                        "broker": {
                            "code": "OTHR",
                            "isOwner": null,
                            "name": null
                        },
                        "datetimeOfArrivalIngarden": 1622571699.767,
                        "id": "arserana",
                        "isSealable": true,
                        "load": null,
                        "mastergardenCode": null,
                        "name": null,
                        "owner": {
                            "code": "WTR",
                            "isOwner": null,
                            "name": null
                        }
                    }
                ]
            },
            {
                "auditOrder": "111",
                "code": "POT 111",
                "name": "POT 111",
                "type": "ProcessingLocation",
                "gardenAssets": [
                    {
                        "annotation": null,
                        "broker": {
                            "code": "CLD",
                            "isOwner": null,
                            "name": null
                        },
                        "datetimeOfArrivalIngarden": 1622746446.932,
                        "id": "Bacrea",
                        "isSealable": true,
                        "load": null,
                        "mastergardenCode": null,
                        "name": null,
                        "owner": {
                            "code": "ICE",
                            "isOwner": null,
                            "name": null
                        },
                        "status": "EMPTY",
                        "type": "JUNK",
                        "unavailable": false,
                        "visitId": "1003768526"
                    }
                ]
            }
        ],
        "logingarden": true,
        "mastergardenCodes": [],
        "gardenCode": "FUN5"
    }
],
"offsitegardens": [],
"gardenAssetsInTransit": []}

代码:

Option Explicit
Dim S as Object, k, Ks as Object
Set S = CreateObject("ScriptControl")
S.Language = "JScript" 
S.addcode "function k(a){var k=[];for(var b in a){k.push('[\'' + b + '\']');}return k;}"

S.Eval ("var J = " & http.ResponseText)
S.Eval ("var L = J.locationsSummaries['0'].locations")
Set Ks = S.Eval("J.locationsSummaries['0'].locations")
For Each K In Ks
    If Not IsNull(S.Eval(K.gardenAssets)) = True Then
        Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1) = "Assets"
    End If

Next K

我需要根据是否有 gardenAssets 从 JSON 中提取不同的信息。但我似乎无法检查数组是否为空。

示例 JSON 无效。对象的最后一个成员或数组的最后一个元素后面不应有逗号。那么你在哪里:

  "broker": {
    "code": "TMTO",
    "isOwner": null,
    "name": null,
  }

"name": null 后不应有逗号 - 在示例 JSON.

中还有多个类似的其他错误

您可以使用在线 JSON 验证器(如 this one)来检测这些错误。理想情况下,您希望修复生成此无效 JSON 的系统,而不是尝试在处理过程中自行更正问题

您可以在 JScript 中使用长度 属性。

    Dim S As Object
    Dim n As Integer, i As Integer, r As Long
    r = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
    Set S = CreateObject("ScriptControl")
    With S
        .Language = "JScript"
        .eval "var J = " & http.ResponseText
        .eval "var A = J.locationsSummaries['0'].locations"
        For n = 1 To S.eval("A.length")
            .eval "var L = A[" & n - 1 & "]"
            For i = 1 To .eval("L.gardenAssets.length")
               Sheet1.Cells(r, 1) = .eval("L.code")
               Sheet1.Cells(r, 2) = .eval("L.gardenAssets[" & i - 1 & "].id")
               r = r + 1
            Next
        Next
    End With