从包含JSON的API链接导入数据到EXCEL

时间:2018-07-31 22:46:45

标签: json excel vba api import

我想将数据从此链接https://api.cartolafc.globo.com/time/slug/umo/16(最后两个引用“ umo”和“ 16”是动态的)导入到spreedsheet中,从而将引用链接到单元格。 我对api或JSON不了解。有没有一种“简单”的方式来做到这一点?

1 个答案:

答案 0 :(得分:1)

首先,您需要使用任何在线JSON查看器(例如http://jsonviewer.stack.hu/)检查JSON响应的结构,在其中您可以看到JSON对象包含atletas数组{{1 }},clubesposicoesstatus对象以及带有标量值的多个属性:

JSON

更进一步,time数组中有一些对象,每个对象都包含一些可以在工作表上填充的属性:

atletas array

这是VBA示例,显示了如何检索这些值。 JSON.bas模块导入VBA项目中以进行JSON处理。

atletas

Option Explicit Sub Test() Dim sJSONString As String Dim vJSON Dim sState As String Dim aData() Dim aHeader() Dim vResult Dim sName ' Retrieve JSON content With CreateObject("MSXML2.XMLHTTP") .Open "GET", "https://api.cartolafc.globo.com/time/slug/umo/16", True .send Do Until .readyState = 4: DoEvents: Loop sJSONString = .responseText End With ' Parse JSON sample JSON.Parse sJSONString, vJSON, sState If sState = "Error" Then MsgBox "Invalid JSON" End End If ' Example of processing single property ' Get 'atletas' array of objects, there is no Set keyword for arrays vResult = vJSON("atletas") '' Optional get 'clubes' object of objects, Set keyword used for objects represented by dictionaries '' Set vResult = vJSON("clubes") ' Convert to 2d array JSON.ToArray vResult, aData, aHeader ' Output 2d array to first worksheet With ThisWorkbook.Sheets(1) .Cells.Delete .Cells.WrapText = False OutputArray .Cells(1, 1), aHeader Output2DArray .Cells(2, 1), aData .Columns.AutoFit End With ' Remove 'atletas' array from root object vJSON.Remove "atletas" ' Remove all worksheets but the first Application.DisplayAlerts = False With ThisWorkbook.Sheets Do Until .Count = 1 .Item(.Count).Delete Loop End With Application.DisplayAlerts = True ' Example of processing multiply properties on separate worksheets ' Processing all the rest of objects and arrays For Each sName In vJSON ' Check if the property is array or object If IsArray(vJSON(sName)) Or IsObject(vJSON(sName)) Then ' Convert to 2d array JSON.ToArray vJSON(sName), aData, aHeader ' Output 2d array to worksheet With ThisWorkbook.Sheets.Add ' Create new worksheet for output OutputArray .Cells(1, 1), aHeader Output2DArray .Cells(2, 1), aData .Columns.AutoFit End With ' Remove output object from root object vJSON.Remove sName End If Next ' Processing all the rest of properties with scalar values which remain in root object ' Convert root object to 2d array JSON.ToArray vJSON, aData, aHeader ' Output 2d array to worksheet With ThisWorkbook.Sheets.Add ' Create new worksheet for output OutputArray .Cells(1, 1), aHeader Output2DArray .Cells(2, 1), aData .Columns.AutoFit End With ' Or the whole JSON structure could be flattened and output to worksheet ' Parse JSON sample JSON.Parse sJSONString, vJSON, sState ' Flatten JSON JSON.Flatten vJSON, vResult ' Convert flattened JSON to 2d array JSON.ToArray vResult, aData, aHeader ' Output 2d array to worksheet With ThisWorkbook.Sheets.Add ' Create new worksheet for output OutputArray .Cells(1, 1), aHeader Output2DArray .Cells(2, 1), aData .Columns.AutoFit End With MsgBox "Completed" End Sub Sub OutputArray(oDstRng As Range, aCells As Variant) With oDstRng .Parent.Select With .Resize(1, UBound(aCells) - LBound(aCells) + 1) .NumberFormat = "@" .Value = aCells End With End With End Sub Sub Output2DArray(oDstRng As Range, aCells As Variant) With oDstRng .Parent.Select With .Resize( _ UBound(aCells, 1) - LBound(aCells, 1) + 1, _ UBound(aCells, 2) - LBound(aCells, 2) + 1) .NumberFormat = "@" .Value = aCells End With End With End Sub 数组对我的输出如下:

enter image description here

顺便说一句,在以下答案中应用了类似的方法:1234567891011121314,{{ 3}},1516171819