2016-07-02 73 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你必須調查。