自助推荐
立即入驻

vb6解析json的方法(利用javascript)

博客3年前更新 桔子雨
2,763 00
vb6解析json的方法(利用javascript)

vb6里不支持json对象,可以通过引用javascript来实现json的解析。

获取简单的json串里的值

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

Set ScriptObj = CreateObject("MSScriptControl.ScriptControl")
ScriptObj.Language = "JavaScript"
ScriptObj.AddCode "var Json = " & JsonStr & ";"

MsgBox ScriptObj.eval("Json.code")

遍历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("MSScriptControl.ScriptControl")
ScriptObj.Language = "JavaScript"
ScriptObj.AddCode "var Json = " & JsonStr & ";var result = """";"

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

sourceData = ScriptObj.eval("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
© 版权声明

相关文章