从VB6中的字符串中获取元素
Take element from a string in VB6
我有一个格式类似于 json 的字符串。由以下代码生成:
str = "{"
str &= Chr(34) & "code" & Chr(34) + ":" & Chr(34) & "0000001" & Chr(34)
str &= Chr(34) & "name" & Chr(34) + ":" & Chr(34) & "product 1" & Chr(34)
str &= Chr(34) & "value" & Chr(34) + ":" & Chr(34) & "150.00" & Chr(34)
str &= "}"
我只需要获取代码、名称和值之后的值。
我找不到一种有效的方法来执行此操作,因为稍后我将不得不概括为更多术语。如果不转换为 JSON,我如何做到这一点?
您提供的代码片段生成此字符串:
{"code":"0000001""name":"product 1""value":"150.00"}
假设你真的在使用VB6来处理这个字符串,下面的代码分解值:
Private Sub Test(ByVal str As String)
Dim groups As Variant
Dim values As Variant
Dim i As Integer
str = Replace(str, "{", "")
str = Replace(str, "}", "")
str = Replace(str, """""", ",")
str = Replace(str, """", "")
groups = Split(str, ",")
For i = LBound(groups) To UBound(groups)
values = Split(groups(i), ":")
Debug.Print values(1)
Next
End Sub
像这样的东西应该有帮助(未经测试):
colon% = 0
Dim completed as Boolean
while completed = false
colon% = InStr(colon% + 1, str, ":")
If colon% > 0 Then
value$ = Mid$(str, colon + 1, InStr(colon% + 2, str, Chr(34)) - colon%)
' ... do whatever with value$ here...
Else
completed = true
End If
Wend
我有一个格式类似于 json 的字符串。由以下代码生成:
str = "{"
str &= Chr(34) & "code" & Chr(34) + ":" & Chr(34) & "0000001" & Chr(34)
str &= Chr(34) & "name" & Chr(34) + ":" & Chr(34) & "product 1" & Chr(34)
str &= Chr(34) & "value" & Chr(34) + ":" & Chr(34) & "150.00" & Chr(34)
str &= "}"
我只需要获取代码、名称和值之后的值。 我找不到一种有效的方法来执行此操作,因为稍后我将不得不概括为更多术语。如果不转换为 JSON,我如何做到这一点?
您提供的代码片段生成此字符串:
{"code":"0000001""name":"product 1""value":"150.00"}
假设你真的在使用VB6来处理这个字符串,下面的代码分解值:
Private Sub Test(ByVal str As String)
Dim groups As Variant
Dim values As Variant
Dim i As Integer
str = Replace(str, "{", "")
str = Replace(str, "}", "")
str = Replace(str, """""", ",")
str = Replace(str, """", "")
groups = Split(str, ",")
For i = LBound(groups) To UBound(groups)
values = Split(groups(i), ":")
Debug.Print values(1)
Next
End Sub
像这样的东西应该有帮助(未经测试):
colon% = 0
Dim completed as Boolean
while completed = false
colon% = InStr(colon% + 1, str, ":")
If colon% > 0 Then
value$ = Mid$(str, colon + 1, InStr(colon% + 2, str, Chr(34)) - colon%)
' ... do whatever with value$ here...
Else
completed = true
End If
Wend