从包含JSON的API链接中导入数据到EXCEL中[英] Import data from an API link that contains JSON to EXCEL

本文是小编为大家收集整理的关于从包含JSON的API链接中导入数据到EXCEL中的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想从此链接中导入数据 https://api. cartolafc.globo.com/time/slug/umo/16 (最后两个参考文献" umo"和" 16"是动态的),将引用链接到单元格. 我没有关于API或JSON的知识.有没有一种"简单"的方法来执行此操作?

推荐答案

首先,您需要使用任何在线JSON查看器(例如> http://jsonviewer.stack.hu/),您可以看到您的JSON对象包含atletas array,clubes,posicoes,posicoes,status,status,time对象和几个属性标量值:

 json

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

 atletas array

这是VBA示例,显示如何检索该值. 导入 json.bas 用于JSON处理的VBA项目.

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

atletas数组的输出如下:

在此处输入图像说明

btw,类似的方法应用其他答案./p>

本文地址:https://www.itbaoku.cn/post/1937821.html

问题描述

I want to import the data from this link https://api.cartolafc.globo.com/time/slug/umo/16 (the last two references, "umo" and "16" are dynamic) to a spreedsheet, linking the references to the cells. I have no knowledgment about api or JSON. Is there a "simple" way to do this?

推荐答案

First of all you need to examine the structure of the JSON response, using any online JSON viewer (e. g. http://jsonviewer.stack.hu/), where you can see that your JSON object contains atletas array, clubes, posicoes, status, time objects, and several properties with scalar values:

JSON

Going further there are objects within atletas array, each of them contains some properties that can be populated on the worksheet:

atletas array

Here is VBA example showing how that values could be retrieved. Import JSON.bas module into the VBA project for JSON processing.

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

The output for atletas array for me is as follows:

enter image description here

BTW, the similar approach applied in other answers.