gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:17:04 30 4
gpt4 key购买 nike

我正在尝试从 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com