VB6下简易的JSON解析器

Public Function JSONParse(ByVal JSONPath As String, ByVal JSONString As String) As Variant
    Dim JSON As Object
    Set JSON = CreateObject("MSScriptControl.ScriptControl")
    JSON.Language = "JScript"
    JSONParse = JSON.eval("JSON=" & JSONString & ";JSON." & JSONPath & ";")
    Set JSON = Nothing
End Function

调用方式,
JSONPath为数据访问路径
JSONString为JSON格式数据
数据内容Data为
{
     "programmers": [{
         "firstName": "Brett",
         "lastName": "McLaughlin",
         "email": "aaaa"
     }, {
         "firstName": "Jason",
         "lastName": "Hunter",
         "email": "bbbb"
     }, {
         "firstName": "Elliotte",
         "lastName": "Harold",
         "email": "cccc"
     }],
     "authors": [{
         "firstName": "Isaac",
         "lastName": "Asimov",
         "genre": "sciencefiction"
     }, {
         "firstName": "Tad",
         "lastName": "Williams",
         "genre": "fantasy"
     }, {
         "firstName": "Frank",
         "lastName": "Peretti",
         "genre": "christianfiction"
     }],
     "musicians": [{
         "firstName": "Eric",
         "lastName": "Clapton",
         "instrument": "guitar"
     }, {
         "firstName": "Sergei",
         "lastName": "Rachmaninoff",
         "instrument": "piano"
     }]
}
-----------------
JSONParse("programmers.length", Data)
3

JSONParse("musicians[1].instrument", Data)
piano

你可能感兴趣的:(杂家杂谈)