从MS Access解析VBA中的JSON(US BLS)

时间:2014-12-31 16:38:21

标签: json vba parsing ms-access vb6

提前感谢您的协助。

我使用的是JSON VB6 Parser,可以在VB JSON Parser

找到

我有以下JSON响应(来自BLS网站,特别是此链接Here

  

{ “状态”: “REQUEST_SUCCEEDED”, “RESPONSETIME”:71, “消息”:[], “结果”:{   “系列”:   [{ “seriesID”: “WPS012”, “数据”:[{ “年”: “2014”, “句号”: “M11”, “periodName”: “11月”, “值”: “153.6”,“脚注“:[{”code“:”P“,”text“:”Preliminary。所有索引都要在原始发布后四个月进行修订。“}]},{”year“:”2014“,”period“:” M10“,”periodName“:”十月“,”值“:”147.4“,”脚注“:[{”代码“:”P“,”文字“:”初步。所有索引都要在原始版本四个月后修订出版物 “}]},{” 年 “:” 2014" , “时期”: “M09”, “periodName”: “九月”, “值”: “146.5”, “脚注”:[{ “代码”: “P”,“text”:“Preliminary。所有索引都在原始发布后四个月进行修订。”}]},{“year”:“2014”,“period”:“M08”,“periodName”:“八月“,”值“:”156.9“,”脚注“:[{”代码“:”P“,”文字“:”初步。所有索引都要在原始出版后四个月进行修订。“}]},{ “年”: “2014”, “句号”: “M07”, “periodName”: “七月”, “值”: “156.4”, “脚注”:[{}]},{ “年”: “2014” , “时期”: “M06”, “periodName”: “六一”, “值”: “179.6”, “脚注”:[{}]},{ “年”: “2014”,“围OD “:” M05" , “periodName”:   “五一”, “值”: “205.4”, “脚注”:[{}]},{ “年”: “2014”, “句号”: “M04”, “periodName”: “四月”, “价值” : “201.6”, “脚注”:[{}]},{ “年”: “2014”, “句号”: “M03”, “periodName”: “三八”, “值”: “188.1”,“脚注“:[{}]},{” 年 “:” 2014" , “句号”: “M02”, “periodName”: “二月”, “值”: “180.2”, “脚注”:[{}]} { “年”: “2014”, “句号”: “M01”, “periodName”: “月”, “值”: “177.8”, “脚注”:[{}]},{ “年”:” 2013" , “时期”: “M12”, “periodName”: “腊”, “值”: “183.2”, “脚注”:[{}]},{ “年”: “2013”​​, “时期”: “M11”, “periodName”: “11月”, “值”: “180.4”, “脚注”:[{}]},{ “年”: “2013”​​, “句号”: “M10”, “periodName” “十月”, “值”: “186.4”, “脚注”:[{}]},{ “年”: “2013”​​, “句号”: “M09”, “periodName”: “九月”,“值“:” 197.1" , “脚注”:[{}]},{ “年”: “2013”​​, “句号”: “M08”, “periodName”: “八一”, “值”: “222.2”,”脚注 “:[{}]},{” 年 “:” 2013" , “句号”: “M07”, “periodName”: “七月”, “值”: “252.9”, “脚注”:[{}] },{ “年”: “2013”​​, “句号”: “M06”, “periodName”: “六一”, “值”: “259.0”, “脚注”:[{}]},{ “年”: “ 2013" , “时期”: “M05”,“P   eriodName “:” 五一”, “值”: “263.7”, “脚注”:[{}]},{ “年”: “2013”​​, “句号”: “M04”, “periodName”: “四月”, “值”: “249.3”, “脚注”:[{}]},{ “年”: “2013”​​, “句号”: “M03”, “periodName”: “三八”, “值”: “268.1” “脚注”:[{}]},{ “年”: “2013”​​, “句号”: “M02”, “periodName”: “二月”, “值”: “267.1”, “脚注”:[{ }]},{ “年”: “2013”​​, “句号”: “M01”, “periodName”: “月”, “值”: “279.7”, “脚注”:[{}]},{“年“:” 2012" , “时期”: “M12”, “periodName”: “腊”, “值”: “283.2”, “脚注”:[{}]},{ “年”: “2012”,”期 “:” M11" , “periodName”: “11月”, “值”: “280.8”, “脚注”:[{}]},{ “年”: “2012”, “句号”: “M10”, “periodName”: “十月”, “值”: “286.7”, “脚注”:[{}]},{ “年”: “2012”, “句号”: “M09”, “periodName”: “九月” “值”: “285.2”, “脚注”:[{}]},{ “年”: “2012”, “句号”: “M08”, “periodName”: “八一”, “值”:“298.9 ”, “脚注”:[{}]},{ “年”: “2012”, “句号”: “M07”, “periodName”: “七月”, “值”: “275.8”, “脚注”: {}]},{ “年”: “2012”, “句号”: “M06”, “periodName”: “六一”, “值”: “226.9”, “脚注”:[{}] },{ “年”: “2012”,“牙周   d “:” M05" , “periodName”: “月”, “值”: “233.7”, “脚注”:[{}]},{ “年”: “2012”, “时期”: “M04”, “periodName”: “四月”, “值”: “239.9”, “脚注”:[{}]},{ “年”: “2012”, “句号”: “M03”, “periodName”: “三八” “值”: “243.6”, “脚注”:[{}]},{ “年”: “2012”, “句号”: “M02”, “periodName”: “二月”, “值”:“239.9 ”, “脚注”:[{}]},{ “年”: “2012”, “句号”: “M01”, “periodName”: “月”, “值”: “243.8”, “脚注”: {}]}]}]   }}`

我能够使用解析器返回“status”,“responseTime”和“message”。除此之外的任何东西(第二个花括号的开头)我什么也得不到。

以下是我尝试使用的代码:

Dim p As Object
Set p = JSON.parse(gbl_response)


'Print the text of a nested property '
Debug.Print p.Item("responseTime")
'Print the text of a property within an array '
Debug.Print p.Item("Results").Item("series").Item("seriesID")

p.Item(“responseTime”)的打印工作并返回“71”,但是在第二次打印尝试时出现“无效的调用过程或参数”错误。

对于我的生活,我一直在寻找并找不到任何解决方案。我试过this看起来几乎完全相同,但是唉,我试图在这里复制解决方案,似乎没有用。

谢谢你的帮助!

1 个答案:

答案 0 :(得分:4)

Public Const jsonSource As String = "{" & _
  """status"": ""REQUEST_SUCCEEDED"", " & _
  """responseTime"": 71, " & _
  """message"": [ " & _
  "], " & _
  """Results"": { " & _
    """series"": [ " & _
      "{ " & _
        """seriesID"": ""WPS012"", " & _
        """data"": [ " & _
          "{ " & _
            """year"": ""2014"", " & _
            """period"": ""M11"", " & _
            """periodName"": ""November"", " & _
            """value"": ""153.6"", " & _
            """footnotes"": [ " & _
              "{ " & _
                """code"": ""P"", " & _
                """text"": ""Preliminary. All indexes are subject to revision four months after original publication."" " & _
              "} " & _
            "] " & _
          "} " & _
        "] " & _
      "}]}}"

Sub JsonTest()
    Dim jsonData As Scripting.Dictionary
    Set jsonData = JSON.parse(jsonSource)

    Dim responseTime As String
    responseTime = jsonData("responseTime")

    Dim results As Scripting.Dictionary
    Set results = jsonData("Results")

    Dim series As Collection
    Set series = results("series")

    Dim seriesItem As Scripting.Dictionary
    For Each seriesItem In series
        Dim seriesId As String
        seriesId = seriesItem("seriesID")
        Debug.Print seriesId

        Dim data As Collection
        Set data = seriesItem("data")

        Dim dataItem As Scripting.Dictionary
        For Each dataItem In data
            Dim year As String
            year = dataItem("year")

            Dim period As String
            period = dataItem("period")

            Dim periodName As String
            periodName = dataItem("periodName")

            Dim value As String
            value = dataItem("value")

            Dim footnotes As Collection
            Set footnotes = dataItem("footnotes")

            Dim footnotesItem As Scripting.Dictionary
            For Each footnotesItem In footnotes
                Dim code As String
                code = footnotesItem("code")

                Dim text As String
                text = footnotesItem("text")

            Next footnotesItem
        Next dataItem
    Next seriesItem
End Sub

enter image description here