kerrypnx
8/9/2018 - 3:29 AM

解析本地josn-18-8-9

解析本地josn-18-8-9

Sub TestJson()
Dim jsstr As String

'vb搞个字符串要加n个引号,真是烦人
jsstr = "{""系别"":""历史系"",""班级"":""一班""," & _
         """学员"":[{""姓名"":""张三"",""年龄"":25,""性别"":""男""}," & _
                   "{""姓名"":""李四"",""年龄"":20,""性别"":""男""}," & _
                   "{""姓名"":""小明"",""年龄"":20,""性别"":""女""}" & _
                  "]" & _
         "}"
        
'前期绑定方便使用智能提示
Dim scobj As New MSScriptControl.ScriptControl
Dim age As Integer

'ScriptControl使用的脚本语言。除了js,也支持Vbscript
scobj.Language = "JavaScript"

'往脚本里添加代码,参数是字符串
scobj.AddCode ("var query = " & jsstr)

'JSON对象获取属性的表示方法:对象.属性
'属性的值如果是个包含多个对象的数组,可以使用索引表示取得对象:对象.属性[0]
'Eval是表达式求值
age = scobj.Eval("query.学员[2].年龄")

Debug.Print "历史系一班的第3个学员的年龄是" & age & "岁"
        
End Sub