在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:VBA-tools/VBA-JSON开源软件地址:https://github.com/VBA-tools/VBA-JSON开源编程语言:Visual Basic .NET 100.0%开源软件介绍:VBA-JSONJSON conversion and parsing for VBA (Windows and Mac Excel, Access, and other Office applications). It grew out of the excellent project vba-json, with additions and improvements made to resolve bugs and improve performance (as part of VBA-Web). Tested in Windows Excel 2013 and Excel for Mac 2011, but should apply to 2007+.
ExamplesDim Json As Object
Set Json = JsonConverter.ParseJson("{""a"":123,""b"":[1,2,3,4],""c"":{""d"":456}}")
' Json("a") -> 123
' Json("b")(2) -> 2
' Json("c")("d") -> 456
Json("c")("e") = 789
Debug.Print JsonConverter.ConvertToJson(Json)
' -> "{"a":123,"b":[1,2,3,4],"c":{"d":456,"e":789}}"
Debug.Print JsonConverter.ConvertToJson(Json, Whitespace:=2)
' -> "{
' "a": 123,
' "b": [
' 1,
' 2,
' 3,
' 4
' ],
' "c": {
' "d": 456,
' "e": 789
' }
' }" ' Advanced example: Read .json file and load into sheet (Windows-only)
' (add reference to Microsoft Scripting Runtime)
' {"values":[{"a":1,"b":2,"c": 3},...]}
Dim FSO As New FileSystemObject
Dim JsonTS As TextStream
Dim JsonText As String
Dim Parsed As Dictionary
' Read .json file
Set JsonTS = FSO.OpenTextFile("example.json", ForReading)
JsonText = JsonTS.ReadAll
JsonTS.Close
' Parse json to Dictionary
' "values" is parsed as Collection
' each item in "values" is parsed as Dictionary
Set Parsed = JsonConverter.ParseJson(JsonText)
' Prepare and write values to sheet
Dim Values As Variant
ReDim Values(Parsed("values").Count, 3)
Dim Value As Dictionary
Dim i As Long
i = 0
For Each Value In Parsed("values")
Values(i, 0) = Value("a")
Values(i, 1) = Value("b")
Values(i, 2) = Value("c")
i = i + 1
Next Value
Sheets("example").Range(Cells(1, 1), Cells(Parsed("values").Count, 3)) = Values OptionsVBA-JSON includes a few options for customizing parsing/conversion if needed:
JsonConverter.JsonOptions.EscapeSolidus = True Installation
Resources |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论