2016-07-02 74 views
1

我已经用scrapy构建了一个蜘蛛。我运行它并在json文件中输出如下所示。然后我使用下面的代码在VBA中使用clsJsonParser。但是我得到了一个3265错误“在这个集合中找不到的元素”element.item(“newstxt”);而element.item(“newstitle”)工作正常。出了什么问题?是我的VBA代码,还是我的json文件的格式?VBA JsonParser clsJsonParser不起作用

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 

末次

[{“newstxt”:“2016年6月21日,20国集团绿色金融研究小组第四次会议在厦门召开,会议由联合主办。来自中国人民银行和英格兰银行的代表,G20国家代表,受邀嘉宾国家和国际组织参加了会议,代表们就G20绿色财务综合报告进行了讨论并原则同意,该报告将提交给6月份G20金融和中央银行副行长厦门会议,研究小组将进一步修改并提交给7月份G20财长和Ce中央银行行长成都会议“),”新一轮“:”\ n G20绿色金融研究小组第四次会议在厦门闭幕\ n“}, {”newstxt“:[”孟买,2016年4月29日\ u00a0-印度为包容性和可持续发展制定雄心勃勃的目标,这需要调动额外的低成本长期资本。联合国环境规划署(UNEP)和印度工商联合会(FICCI)今天发布的一份新报告显示了该国如何引入创新方法吸引私人资本用于绿色资产 - 并概述了一些关键问题“],”newstitle“:”\ n新报告显示印度如何扩大可持续金融\ n“}]

回答

1

不能说,但如果你运行这个测试功能:

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

它将打印:

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你必须调查。