gpt4 book ai didi

json - VBA:将 JSON 打印到文本文件包括双引号

转载 作者:行者123 更新时间:2023-12-04 21:14:37 24 4
gpt4 key购买 nike

以下代码

Dim theJSON As String
theJSON = "{""docType"":""DRAFT"",""riskState"":""NY""}"

theFileName = "C:\test.json"

Open theFileName For Output As #1
Write #1, theJSON 'jsonSerialization
Close #1

产生这样的输出:

"{""docType"":""DRAFT"",""riskState"":""NY""}"



我无法弄清楚如何阻止 VBA 字符串语法打印到文件中(转义引号和前导/尾随引号)。

有任何想法吗?

最佳答案

VBA 有两组不同的文本文件处理函数:
Write #Input #旨在与“结构化”文件 (.csv) 一起使用,并使用文本限定数据 - 任何 StringVariant带有字符串子类型的双引号转义为 "" , 整个事情都用引号括起来。
Print #Input(number, #filenumber)旨在与“非结构化”文件一起使用。

解决方法是使用 Print而不是 Write :

Dim theJSON As String
theJSON = "{""docType"":""DRAFT"",""riskState"":""NY""}"

theFileName = "C:\test.json"

Dim handle As Integer
handle = FreeFile
Open theFileName For Output As #handle
Print #handle, theJSON 'jsonSerialization
Close #handle

请注意,您应该始终使用 FreeFile函数返回一个打开的文件句柄,而不是硬编码数字 - 这避免了句柄冲突和意外文件访问的可能性。

关于json - VBA:将 JSON 打印到文本文件包括双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52710514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com