【发布时间】:2018-10-23 11:25:39
【问题描述】:
我正在阅读VBA-JSON的源代码,位于:
https://github.com/VBA-tools/VBA-JSON/blob/master/JsonConverter.bas
在将 JSON 对象(以“{”字符开头)转储到内存中的函数中:
Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Dictionary
Dim json_Key As String
Dim json_NextChar As String
Set json_ParseObject = New Dictionary
json_SkipSpaces json_String, json_Index
If VBA.Mid$(json_String, json_Index, 1) <> "{" Then
Err.Raise 10001, "JSONConverter", json_ParseErrorMessage(json_String, json_Index, "Expecting '{'")
Else
json_Index = json_Index + 1
Do
json_SkipSpaces json_String, json_Index
If VBA.Mid$(json_String, json_Index, 1) = "}" Then
json_Index = json_Index + 1
Exit Function
ElseIf VBA.Mid$(json_String, json_Index, 1) = "," Then
json_Index = json_Index + 1
json_SkipSpaces json_String, json_Index
End If
json_Key = json_ParseKey(json_String, json_Index)
json_NextChar = json_Peek(json_String, json_Index)
If json_NextChar = "[" Or json_NextChar = "{" Then
Set json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
Else
json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
End If
Loop
End If
End Function
有什么区别:
Set json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
还有以下的:
json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
我在SO里搜了一下,发现了这个帖子,并没有打消疑惑:
What does the keyword Set actually do in VBA?
通过阅读代码,我了解到如果解析器读取“{”或“[”,那么它需要使用 Dictionary 来保存“{”和“}”之间的任何内容,并使用 Collection 来保存“{”和“}”之间的任何内容“[”和“]”,如果解析器读取的不是这两个,那么它只是一个值,可以是布尔值或字符串或整数等。
我不明白 Set 和 Assignment 之间的区别。 "Set" 将复制 json_ParseValue 的返回变量的地址,而赋值只是复制返回变量的另一个副本。是不是因为在第一种情况下,还需要修改返回的对象,所以它必须通过地址传递(就像在 C++ 中我们使用 &)?
【问题讨论】: