网络流传的文章中,多数使用脚本控件来解析json字符串,在vbs增加一个解析函数,然后调用即可。

Function JsonDecode(JSONString, JSONPath)
    'by邓伟,基于js脚本解析器实现的json解析,传入需要访问的json路径,返回json节点值。路径语法同js
    '    Dim strJson: strJson = "{""a"":[{""b"":1},{""b"":2}]}"
    '    MsgBox JsonDecode(strJson, "a[1].b")
    Dim json
    Set json = CreateObject("MSScriptControl.ScriptControl")
    json.Language = "JScript"
    If JSONPath = "" Then
        JsonDecode = json.Eval(JSONString)
    Else
        JsonDecode = json.Eval("JSON=" & JSONString & ";JSON." & JSONPath & ";")
    End If
    Set json = Nothing
End Function

'用法:比如有一个包含数组嵌套的 json 字符串,需要取数组里面的值
    Dim strJson: strJson = "{""a"":[{""b"":1},{""b"":2}],""c"":""dengwei"",""d"":{""aa"":11}}"
    '遇到数组嵌套的,可以用变量去循环得到每个数组路径的值
    Dim i: i = 0
    MsgBox JsonDecode(strJson, "a[" & i & "].b")
    MsgBox JsonDecode(strJson, "a[1].b")
    MsgBox JsonDecode(strJson, "c")
    MsgBox JsonDecode(strJson, "d.aa")

增加一个从文本文件载入文本到变量的函数

    ' 调用函数并显示文件内容
    Dim filePath, textContent, i: i = 0
    filePath = "D:\code\inc\test.json"                                          ' 请将此路径替换为您的实际文件路径
    textContent = LoadTextFromFile(filePath)
    MsgBox JsonDecode(textContent, "Item[" & i & "].Model.ChannelAddr")


' 定义从文本文件加载字符串到变量的函数
Function LoadTextFromFile(filePath)
    Dim fso, file, fileContent
    
    ' 创建文件系统对象
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' 检查文件是否存在
    If fso.FileExists(filePath) Then
        ' 打开文件并读取内容
        Set file = fso.OpenTextFile(filePath, 1)                                ' 1 表示以只读方式打开文件
        fileContent = file.ReadAll
        file.Close
        Set file = Nothing
    Else
        ' 文件不存在,返回空字符串或错误信息
        fileContent = ""
        MsgBox "文件不存在: " & filePath
    End If
    
    ' 释放文件系统对象
    Set fso = Nothing
    
    LoadTextFromFile = fileContent
End Function



Function JsonDecode(JSONString, JSONPath)
    'by邓伟,基于js脚本解析器实现的json解析,传入需要访问的json路径,返回json节点值。路径语法同js
    '    Dim strJson: strJson = "{""a"":[{""b"":1},{""b"":2}]}"
    '    MsgBox JsonDecode(strJson, "a[1].b")
    Dim json
    Set json = CreateObject("MSScriptControl.ScriptControl")
    json.Language = "JScript"
    If JSONPath = "" Then
        JsonDecode = json.Eval(JSONString)
    Else
        JsonDecode = json.Eval("JSON=" & JSONString & ";JSON." & JSONPath & ";")
    End If
    Set json = Nothing
End Function

对于数组节点,可以通过获取数组的长度值,然后使用 for 语句遍历所有元素

Option Explicit


    
    ' 调用函数并显示文件内容
    Dim filePath, textContent, i: i = 0
    filePath = "D:\code\inc\test.json"                                          ' 请将此路径替换为您的实际文件路径
    textContent = LoadTextFromFile(filePath)
    MsgBox JsonDecode(textContent, "Item[" & i & "].Model.ChannelAddr")
    '循环遍历数组,先取得数组长(即数组的 length 属性)度,然后 for 语句实现
    Dim ArrLen: ArrLen = JsonDecode(textContent, "Item[0].Rows.length")
    For i = 0 To ArrLen - 1
        MsgBox "第 " & (i + 1) & " 个元素值:" & _
            JsonDecode(textContent, "Item[0].Rows[" & i & "].AccessAddress")
    Next


' 定义从文本文件加载字符串到变量的函数
Function LoadTextFromFile(filePath)
    Dim fso, file, fileContent
    
    ' 创建文件系统对象
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' 检查文件是否存在
    If fso.FileExists(filePath) Then
        ' 打开文件并读取内容
        Set file = fso.OpenTextFile(filePath, 1)                                ' 1 表示以只读方式打开文件
        fileContent = file.ReadAll
        file.Close
        Set file = Nothing
    Else
        ' 文件不存在,返回空字符串或错误信息
        fileContent = ""
        MsgBox "文件不存在: " & filePath
    End If
    
    ' 释放文件系统对象
    Set fso = Nothing
    
    LoadTextFromFile = fileContent
End Function



Function JsonDecode(JSONString, JSONPath)
    'by邓伟,基于js脚本解析器实现的json解析,传入需要访问的json路径,返回json节点值。路径语法同js
    '    Dim strJson: strJson = "{""a"":[{""b"":1},{""b"":2}]}"
    '    MsgBox JsonDecode(strJson, "a[1].b")
    Dim json
    Set json = CreateObject("MSScriptControl.ScriptControl")
    json.Language = "JScript"
    If JSONPath = "" Then
        JsonDecode = json.Eval(JSONString)
    Else
        JsonDecode = json.Eval("JSON=" & JSONString & ";JSON." & JSONPath & ";")
    End If
    Set json = Nothing
End Function

Views: 51

Hi, I’m 邓伟

本来无一物,何处惹尘埃

发表回复