简体   繁体   English

Excel数组的大小-JSON VBA解析器

[英]size of an array excel - json vba parser

I am using a vba json parser : https://github.com/VBA-tools/VBA-JSON . 我正在使用vba json解析器: https : //github.com/VBA-tools/VBA-JSON I want to loop over the elements in the B array but I am unsure how to do this. 我想遍历B数组中的元素,但不确定如何执行此操作。 eg 例如

Set Json = JsonConverter.ParseJSON("{""a"":123,""b"":[1,2,3,4],""c"":{""d"":456}}")

If you want to get back the number of elements in B how do you do this? 如果要获取B中元素的数量,该怎么做?

You get back the actual value by doing the following : Json("a") 您可以通过执行以下操作来获取实际值: Json("a")

The docs for the source vba-json state: vba-json状态的文档:

parse JSON and create Dictionary/Collection 解析JSON并创建Dictionary / Collection

So you will get back one of those objects. 因此,您将取回这些对象之一。 This seems to work: 这似乎可行:

Sub testJson()

Dim Json As Object
Set Json = JsonConverter.ParseJson("{""a"":123,""b"":[1,2,3,4],""c"":{""d"":456}}")

Debug.Print Json("a") ' -> 123
Debug.Print Json("b")(2) ' -> 2
Debug.Print Json("c")("d") ' -> 456
Json("c")("e") = 789

Dim var As Object
    ' Get the object from Json
    Set var = Json("b")
    ' Both Dictionary and Collection support the Count property
    Debug.Print var.Count

Dim elem As Variant
    For Each elem In var
        Debug.Print elem
    Next elem

Debug.Print JsonConverter.ConvertToJson(Json)
' -> "{""a"":123,""b"":[1,2,3,4],""c"":{""d"":456,""e"":789}}"

End Sub

The "b" in the Json example returns a collection but for "c" you would get back a dictionary. Json示例中的“ b”返回一个集合,但是对于“ c”,您将获得一个字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM