首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA JsonParser clsJsonParser不工作

VBA JsonParser clsJsonParser不工作
EN

Stack Overflow用户
提问于 2016-07-02 10:22:13
回答 1查看 1.7K关注 0票数 1

我做了一只有刮痕的蜘蛛。我运行它,并将输出放在json文件中,如下所示。然后,我在VBA中使用clsJsonParser,代码如下。但是对于element.item(“newstxt”),我得到了一个3265错误元素“在这个集合中找不到”;而element.item("newstitle")工作得很好。出什么问题了?是我的VBA代码,还是json文件的格式?

代码语言:javascript
复制
Public Sub JSONImport()
Dim coll As Collection
Dim json As New clsJSONparser
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim element As Variant

Dim FileNum As Integer
Dim DataLine As String, jsonStr As String

' READ FROM EXTERNAL FILE
FileNum = FreeFile()
Open "C:\Users\Philippe\sfo\unepinquiry\items.json" For Input As #FileNum

' PARSE FILE STRING
jsonStr = ""
While Not EOF(FileNum)
    Line Input #FileNum, DataLine

    jsonStr = jsonStr & DataLine & vbNewLine
Wend
Close #FileNum

    Set db = CurrentDb
    Set rs = db.OpenRecordset("News_1", dbOpenDynaset, dbSeeChanges)
    Set coll = json.parse(jsonStr)

    For Each element In coll
        rs.AddNew
        rs!newstitle = element.item("newstitle")
        rs!newstxt = element.item("newstxt")
        rs.Update
    Next


Set element = Nothing
Set coll = Nothing

结束子对象

{“新闻报”:“2016年6月21日,G20绿色金融研究组第四次会议在厦门举行。会议由中国人民银行和英格兰银行的共同主席主持。来自G20国家、受邀国家和国际组织的代表参加了会议。与会代表就G20绿色金融综合报告进行了原则上的讨论和商定,该报告将提交6月G20金融和中央银行厦门代表会议。该研究小组将进一步修订并向7月G20财政部长和央行行长成都会议提交报告。“新议程”:“G20绿色金融研究小组第四次会议在厦门结束”{“新报告”:“2016年4月29日,孟买--印度为包容性和可持续发展制定了雄心勃勃的目标,这需要调动额外的低成本、长期资本。”联合国环境规划署(环境署)和印度工商会联合会(FICCI)今天发布的一份新报告显示,印度已经在采取创新办法,吸引私人资本投资于绿色资产,并概述了印度深化这一进程的若干关键步骤。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-02 11:49:04

无法判断,但如果运行此测试函数:

代码语言:javascript
复制
Public Sub TestJsonText()

    Dim DataCollection      As Collection
    Dim ResponseText        As String

    ResponseText = _
        "[{""newstxt"": [""On June 21, 2016, the fourth meeting of the G20 Green Finance Study Group was held in Xiamen. The meeting was hosted by Co-chairs from the People's Bank of China and the Bank of England. Delegates from G20 countries, invited guest countries and International Organizations participated in the meeting. The delegates discussed and agreed in principle on the G20 Green Finance Synthesis Report, which would be submitted to the June G20 Finance and Central Bank Deputies Xiamen Meeting. The study group will further revise and submit the Report to the July G20 Finance Ministers and Central Bank Governors Chengdu Meeting.""]," & _
        """newstitle"": ""\nFourth Meeting of the G20 Green Finance Study Group Concludes in Xiamen\n""}, " & _
        "{""newstxt"": [""Mumbai, 29 April 2016\u00a0- India has set ambitious goals for inclusive and sustainable development, which require the mobilization of additional low-cost, long-term capital. A new report launched today by the United Nations Environment Programme (UNEP) and the Federation of Indian Chambers of Commerce and Industry (FICCI) shows how the country is already introducing innovative approaches to attract private capital for green assets - and outlines a number of key steps to deepen this process in India.""]," & _
        """newstitle"": ""\nNew Report Shows How India Can Scale up Sustainable Finance\n""}]"
    If ResponseText <> "" Then
        Set DataCollection = CollectJson(ResponseText)
        MsgBox "Retrieved" & Str(DataCollection.Count) & " root member(s)", vbInformation + vbOKOnly, "Web Service Success"
    End If

    Call ListFieldNames(DataCollection)

    Set DataCollection = Nothing

End Sub

使用这里找到的Json模块:VBA.CVRAPI

它将印刷:

代码语言:javascript
复制
root                        
    0                       
        newstxt             On June 21, 2016, the fourth meeting of the G20 Green Finance Study Group was held in Xiamen. The meeting was hosted by Co-chairs from the People's Bank of China and the Bank of England. Delegates from G20 countries, invited guest countries and International Organizations participated in the meeting. The delegates discussed and agreed in principle on the G20 Green Finance Synthesis Report, which would be submitted to the June G20 Finance and Central Bank Deputies Xiamen Meeting. The study group will further revise and submit the Report to the July G20 Finance Ministers and Central Bank Governors Chengdu Meeting.
        newstitle           
Fourth Meeting of the G20 Green Finance Study Group Concludes in Xiamen

    1                       
        newstxt             Mumbai, 29 April 2016 - India has set ambitious goals for inclusive and sustainable development, which require the mobilization of additional low-cost, long-term capital. A new report launched today by the United Nations Environment Programme (UNEP) and the Federation of Indian Chambers of Commerce and Industry (FICCI) shows how the country is already introducing innovative approaches to attract private capital for green assets - and outlines a number of key steps to deepen this process in India.
        newstitle           
New Report Shows How India Can Scale up Sustainable Finance

在我看来是对的。所以这可能是clsJSONparser,你必须调查。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38159050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档