修改 VBA JSON 文件



我想使用 vba 在 excel 中修改 JSON 文件。

所以我有这个 JSON 文件

{
"root": [{
"STATUS_RESPONSE": {
"STATUS": {
"STATUS": {
"OWNER": "root",
}
},
"REQ_ID": "00000",
"RESULT": [{
"USER": {
"BUSINESS_ID": "A",
"USER_NUMBER": "45",
"LANGUAGE": "F",
}
},
{
"USER_SESSION": {
"USER_ID": "0000001009",
"HELP_URL": "http://google.com",
}
},
{
"USER_ACCESS": {
"SERVICES_ROLE": "true",
"JOURNALLING": "true",
}
}]
}
}]
}

我只想修改"BUSINESS_ID">

然后我可以使用这个导出到同一个 JSON 文件

Private Sub CommandButton2_Click()
Dim rng As Range, items As New Collection, myitem As New Dictionary, i As Integer, cell As Variant, myfile As String
Dim FSO As New FileSystemObject
Dim buss As String
Dim JsonTS As TextStream
Set rng = Range("A2")
Set JsonTS = FSO.OpenTextFile("test.json", ForReading)
JsonText = JsonTS.ReadAll
JsonTS.Close
Set JSON = ParseJson(JsonText)
JSON("root")(1)("STATUS_RESPONSE")("RESULT")(1)("USER")("BUSINESS_ID") = Sheets(1).Cells(2, 1).Value
buss = JSON("root")(1)("STATUS_RESPONSE")("RESULT")(1)("USER")("BUSINESS_ID")
myfile = "test.json"
Open myfile For Output As #1
Write #1, buss
Close #1
End Sub

我可以编辑单元格,这将替换 JSON 文件,但它会从上面的 JSON 文件中带走整个结构。

如果我将业务 ID 更改为 C,我会得到类似于 json 文件的内容:

"C"

有没有办法我可以修改现有文件中我需要的东西而不会消失其他所有内容

您应该导出整个JSON对象,而不仅仅是其中的一部分。

Write #1, JsonConverter.ConvertToJson(JSON, Whitespace:=2)

最新更新