2017-06-15 152 views
0

我一直試圖從一個API解析一些JSON數據作爲更大的項目的一部分,並且由於我對VBA/Json的經驗有限,我很難計算看看我的VBA代碼出現了什麼問題。VBA Json Parse-解析器沒有正確分解Json

我已經使用了下面的VBA Json解析器,並且基本都停止了在同一點工作,並給我一個「不完整的解析」。 VBA的JSON https://code.google.com/archive/p/vba-json vbjson www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680

JSON的,我試圖解析如下:

{"links":[],"content":{"carrier":{"allowedToOperate":"Y","bipdInsuranceOnFile":"1000","bipdInsuranceRequired":"Y","bipdRequiredAmount":"750","bondInsuranceOnFile":"0","bondInsuranceRequired":"N","brokerAuthorityStatus":"N","cargoInsuranceOnFile":"0","cargoInsuranceRequired":"N","carrierOperation":{"carrierOperationCode":"A","carrierOperationDesc":"Interstate"},"commonAuthorityStatus":"A","contractAuthorityStatus":"A","crashTotal":19,"dbaName":null,"dotNumber":124871,"driverInsp":974,"driverOosInsp":33,"driverOosRate":3.38809034907597535934291581108829568789,"driverOosRateNationalAverage":"5.51","ein":460396676,"fatalCrash":0,"hazmatInsp":0,"hazmatOosInsp":0,"hazmatOosRate":0,"hazmatOosRateNationalAverage":"4.5","injCrash":6,"isPassengerCarrier":"N","issScore":null,"legalName":"A&A EXPRESS LLC","oosDate":null,"oosRateNationalAverageYear":"2009-2010","phone":"6055822402","phyCity":"BRANDON","phyCountry":"US","phyState":"SD","phyStreet":"1015 9TH AVENUE NORTH","phyZipcode":"57005","reviewDate":"2016-09-23","reviewType":"N","safetyRating":"S","safetyRatingDate":"2008-08-29","safetyReviewDate":"2016-09-23","safetyReviewType":"N","snapshotDate":"1485493200000","statusCode":"A","totalDrivers":237,"totalPowerUnits":253,"towawayCrash":13,"vehicleInsp":477,"vehicleOosInsp":58,"vehicleOosRate":12.15932914046121593291404612159329140461,"vehicleOosRateNationalAverage":"20.72"},"links" 

我的VBA代碼如下所示:

Public Sub PARSEJSON() 
Dim reader As New XMLHTTP60 
Dim api As Object 
Dim WS As Worksheet 
Dim item As Variant 

reader.Open "GET", "https://mobile.fmcsa.dot.gov/qc/services/carriers/124871?webKey=APIKEY", False 
reader.send 

    Set WS = ActiveSheet 
    Set api = JSON.parse(reader.responseText) 
     For Each item In api 
      WS.Range("a3").Value = item("") 


     Next 


End Sub 

我不能進入的JSON引用我需要的值,因爲解析器產生類似的結果:

photo of watcher in VBA

它看起來像Json解析器只是從API中分解出Json字符串的一部分,它來自政府網站,所以我認爲它應該採用通用的Json格式。我希望我在這篇文章中包含了足夠的細節以涵蓋我的問題的範圍

+0

請檢查您的JSON字符串是有效的。 – PMerlet

+0

我在https://jsonlint.com/檢查了字符串,它說它有效的Json! @Cubi – hankmoodie

回答

0

我解決了這個問題,我進一步研究瞭如何引用字典,並意識到我使用了錯誤的語法來引用和添加額外的步驟不需要。

新VBA代碼產生的結果我想:

Public Sub PARSEJSON() 
Dim reader As New XMLHTTP60 
Dim dict As Object 
Dim api As New Scripting.Dictionary 
Dim json As New Class1 
Dim WS As Worksheet 
Dim key As Variant 
Dim item As Variant 



reader.Open "GET", "https://mobile.fmcsa.dot.gov/qc/services/carriers/124871?webKey=APIKEY", False 
reader.send 

    Set WS = ActiveSheet 
    Set api = json.parse(reader.responseText) 
     MsgBox api("content")("carrier").Count 
     MsgBox api.item("content")("carrier")("driverOosInsp") 
     WS.Range("a1").value = api.item("content")("carrier")("driverOosInsp") 
End Sub