vb6里解析json数据

时间:2024-10-18 07:16:41

vb6里不支持json对象,这里使用通过引用js来实现json的解析

获取简单的json串里的值

Dim ScriptObj As Object
Dim JsonStr As String
JsonStr = "{""code"":""0"",""msg"":""SUCCESS""}"

Set ScriptObj = CreateObject("")
 = "JavaScript"
 "var Json = " & JsonStr & ";"

MsgBox ("")

遍历json键值对较多的复杂json串

Dim ScriptObj As Object
Dim JsonStr, sourceData As String
JsonStr = "{""code"":""0"",""msg"":""SUCCESS"",""data": _
{""AAA"":""111"",""BBB"",""222"",""CCC"",""333"",……}"}"

Set ScriptObj = CreateObject("")
 = "JavaScript"
 "var Json = " & JsonStr & ";var result = """";"

'执行js语句,遍历json对象,可嵌套循环
 ("for (var key in Json.data) {result += ""|"" + key + "":"" + Json.data[key];}") 

sourceData = ("lockinfo")

Dim arrStr() As String
Dim temStr As Variant

arrStr = Split(sourceData, "|")'解析拼接的字符串
For Each temStr In arrStr
    If temStr <> Empty Then
		msgbox Split(temStr, ":")(0) & ":" & Split(temStr, ":")(1)
    End If
Next

但是第二种方法性能较差,适用场景有限