vba json读取子数组如何读取

时间:2018-11-10 06:27:23

标签: json excel vba excel-vba web-scraping

API使用下面的json数据

[{
"Id":"8",
"Date":"12/11/2018",


"readings":[{"Id":"61","DailyLogBookID":"8","Substation":"MS-1","LoadMax":"898","LoadMin":"9898","VoltageMax":"98","VoltageMin":"98","FrequencyMax":"988","FrequencyMin":"9898","PFMax":"988","PFMin":"989","PowerDip":"9898","Remarks":"9898"},

{"Id":"62","DailyLogBookID":"8","Substation":"MS-2","LoadMax":"9889","LoadMin":"98","VoltageMax":"98","VoltageMin":"889","FrequencyMax":"9898","FrequencyMin":"98","PFMax":"98","PFMin":"98","PowerDip":"98","Remarks":"98"}  
]}]

/*************VBA Code************/

Sub CallChildDate(id As String) 
Dim http As Object, JSON As Object, i, j As Integer 
Set http = CreateObject("MSXML2.XMLHTTP") 
Dim strUrl As String 
http.Open "GET", id, False 
http.send 
Set JSON = ParseJson(http.responseText) 
i = 1 

For Each item In JSON
Sheets(1).Cells(i, 1).Value = item("Id") 
Sheets(1).Cells(i, 2).Value = item("Date")
i = i + 1 
Next

End Sub

如何在“读数”数据上方获取数据,将其分配给单元格值。如何在单元格中使用assn来“读取”数组数据。

1 个答案:

答案 0 :(得分:0)

我注意到您现在已经编辑了JSON和问题:

根据您先前的问题:您快到了。下面,我从文件中读取JSON。

{意味着一个字典,因此您可以通过键访问,[意味着一个集合,因此您可以通过索引访问。 ""表示字符串文字,因此您按原样阅读。我使用JSON parser处理从文件读取的JSON。将.bas从该链接添加到您的项目后,您可以通过VBE>工具>引用> Microsoft脚本运行时添加引用。

如果您检查JSON结构以查看idreadings的位置,您将看到初始对象是一个集合。 readings在该集合的第一项中,该项​​是字典。 readings是返回字典集合的键:

Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 

因此,您正在下面的代码中遍历字典的集合,其中每个item是一个字典。您可以循环字典关键字并访问并使用其中的每个字典的关键字(例如id)来检索关联的值。

Option Explicit

Public Sub ReadValues()
    Dim fso As Object, stream As Object, jsonText As String, item As Object
    Dim json As Object, ws As Worksheet, i As Long
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set stream = fso.OpenTextFile("C:\Users\HarrisQ\Desktop\test.json", ForReading)
    jsonText = stream.ReadAll
    stream.Close

    Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 'Collection of dictionaries

    Dim c As Long, key As Variant, arr(), r As Long

    With ws
        arr() = json(1).keys
        .Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
        r = 2
        For Each item In json
            c = 1
            For Each key In item
                .Cells(r, c).Value = item(key)
                c = c + 1
            Next
            r = r + 1
        Next
    End With
End Sub

关于您的修订问题:

Option Explicit
Public Sub CallChildDate(id As String)
    Dim json As Object, item As Object, ws As Worksheet, i As Long

    Set ws = ThisWorkbook.Worksheets("Sheet1")

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", id, False  '<==Assuming id is actually a URL and not to be concantenated with your former variable strURL.
       .send
       Set json = JsonConverter.ParseJson(.responseText)(1)("readings") 'Collection of dictionaries
    End With

    Dim c As Long, key As Variant, arr(), r As Long

    With ws
        arr() = json(1).keys
        .Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
        r = 2
        For Each item In json
            c = 1
            For Each key In item
                .Cells(r, c).Value = item(key)
                c = c + 1
            Next
            r = r + 1
        Next
    End With
End Sub

使用您的初始JSON字符串输出:

enter image description here