json - 将特定 JSON 字段从 .responseText 提取到单个 Excel 单元格

标签 json excel vba api web-scraping

我正在尝试从 JSON 检索特定字段、resolve。我不确定如何才能获得这一领域。我添加了 Msgbox [Exists & Fail] 以查看代码是否能够读取单元格内的单词“resolve”,但是我返回失败。

有什么方法可以只获得字段解析吗?请协助。

谢谢!

 TargetURL = "https://api.passivetotal.org/v2/dns/passive?query=passivetotal.org"
    actionType = "Content-Type"
    actionWord = "application/json"
    With CreateObject("Microsoft.XMLHTTP")
        .Open "GET", TargetURL, False
        .setRequestHeader actionType, actionWord
        .setRequestHeader "Authorization", "Basic <Encoded 64>"
        .send
        If .Status = 200 Then
            Sheets(6).Cells(Count, 10).Value = "Connected"
            Debug.Print .responseText
            MsgBox .responseText
            Set JSON = ParseJson(.responseText)
            Sheets(6).Cells(Count, 8).Value = .responseText
            If Sheets(6).Cells(Count, 8).Value = ("resolve") Then
                MsgBox ("Exists")
            Else
                MsgBox ("Fail")
            End If
        Else
            MsgBox .Status & ": " & .StatusText
        End If
    End With

最佳答案

解析 JSON 响应:

以下内容从文件中读取结果 json 并解析出每个解析。它使用JSONConverter.bas。请注意,我已在 python 脚本中提取了“结果”JSON 集合,这与您通过 Set json = JsonConverter.ParseJson 对转换后的 JSON 字符串执行 json("results") 的操作相同(.responseText)(“结果”)

JSONConverter.bas添加到您的项目后,您需要转到工具>引用>添加对Microsoft Scripting Runtime的引用

Option Explicit
Public Sub GetJSONExtract()
    Dim fso As Object, jsonFile As Object, jsonText As String, json As Object, item As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set jsonFile = fso.OpenTextFile("C:\Users\User\Desktop\Sample.json")
    jsonText = jsonFile.ReadAll
    Set json = JsonConverter.ParseJson(jsonText)  '<== Using results collection
    'Set json = JsonConverter.ParseJson(.responseText)("results")  '<== In your vba XMLHTTP version
    For Each item In json
        Debug.Print item("resolve")
    Next
End Sub

正如您所了解的,我已经展示了如何解析 JSON。


附加说明:

我实际上使用了如下所示的python脚本;改编自 API 文档。然后,我添加了一些代码,将响应写入 JSON 文件以供以后导入。使用 Anaconda/Spyder 运行。

import requests
import json

username = 'xxx'
key = 'yyy'
auth = (username, key)
base_url = 'https://api.passivetotal.org'

def passivetotal_get(path, query):
    url = base_url + path
    data = {'query': query}
    response = requests.get(url, auth=auth, json=data)
    return response.json()

pdns_results = passivetotal_get('/v2/dns/passive', 'passivetotal.org')

for resolve in pdns_results['results']:
   print('Found resolution: {}'.format(resolve['resolve']))


with open(r"C:\Users\User\Desktop\Output.json", "w") as text_file:
    text_file.write(json.dumps(pdns_results['results']))

打印出所有解析。

原始返回的 JSON 结构如下所示:

structure

返回的对象是字典的集合。您可以通过字典键“resolve”

访问所需的值

关于json - 将特定 JSON 字段从 .responseText 提取到单个 Excel 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52866031/

相关文章:

当curl返回200时,VBA GET请求返回404

vba - Excel 查找数字组合的函数

vba - 使用VBA将图片插入Excel并保持宽高比不超过尺寸

c# - 在 C# 中使用 json 对象

java - 创建用于读取/写入HDFS数据的JSON服务

php - 如何根据对象的键(而不是整个对象)更新列表中的对象?

java - 将固定大小的 Map 序列化为 CBOR

excel - 从excel vba中的数组中查找最高日期

Excel 索引与偏移/间接 - 我不明白为什么在这种情况下偏移/间接更快

excel - 运行时错误 '438' 问题使用 VBA 访问 activeX 控件