使用VBA-JSON分析多个JSON/VBA对象



我有以下JSON响应:

[
{
"id": 1354345345,
"date": "2021-10-01T23:29:42.000000+02:00",
"count": 0,
"year": 2020,
"area": 232,
"numberList": [
8693978
],
"Property": {
"PropertyID": 135005860000118,
"PropertyNumber": 2244
}
},
{
"id": 2345235345,
"date": "2021-02-13T13:40:30.000000+01:00",
"count": 2,
"year": 2020,
"area": 122,
"numberList": [
8693978
],
"Property": {
"PropertyID": 153005860001536,
"PropertyNumber": 1555
}
}
]

在数组[]内部,可以有几个对象{},每个对象的值都在"中;区域";。在本例中,数组中有两个对象。

我正在尝试通过VBA-JSON解析JSON。

我尝试过以下VBA代码,但它只返回每个对象项的名称,而不返回值。

Set Json = JsonConverter.ParseJson(responseText)
For Each Item In Json
For Each i In Item
Debug.Print i
Next
Next

VBA调试控制台将显示:

id
date
count
year
area
numberList
Property
id
date
count
year
area
numberList
Property

如何获取每个对象的面积?

JsonConverter.ParseJson(responseText)创建一个对象来保存ScriptingDictionaries。。。因此,它们会暴露密钥和项目。基于这种逻辑,请尝试下一种方式";区域";值提取:

  1. 您显示的字符串缺少一个":"字符(替换为"(。此处:"区域";;232,。解析它应该是正确的。。。

  2. 下一个代码版本将在对象键和项之间迭代,提取键"的值(项(;区域":

Dim json As Object, strFile As String, responseText As String, dict, i As Long

responseText = "the corrected string you show..."

Set json = JsonConverter.ParseJSON(responseText)

For Each dict In json
For i = 0 To dict.count - 1
If dict.Keys()(i) = "area" Then
Debug.Print TypeName(dict), TypeName(dict.Keys()(i)), TypeName(dict.Items()(i))
Debug.Print dict.Items()(i)
End If
Next i
Next dict

最新更新