I am using a vba json parser : 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. e.g.
Set Json = JsonConverter.ParseJSON("{""a"":123,""b"":[1,2,3,4],""c"":{""d"":456}}")
Run Code Online (Sandbox Code Playgroud)
If you want to get back the number of elements in B how do you do this?
You get back the actual value by doing the following : Json("a")
源vba-json状态的文档:
解析 JSON 并创建字典/集合
因此,您将取回其中一个对象。这似乎有效:
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
Run Code Online (Sandbox Code Playgroud)
Json 示例中的“b”返回一个集合,但对于“c”,您将返回一个字典。