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
未经允许不得转载:桔子雨工作室 » vb6解析json的方法(利用javascript)
分享到: 生成海报
一个数字化服务提供商

承接外贸建站,软件APP开发

部分产品联系我们
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码