将特定JSON字段从.responseText提取到单个excel单元格
我试图从JSON中检索一个特定字段resolve。我不知道如何才能得到那一块地。我添加了Msgbox[Exists&Fail]以查看代码是否能够读取单元格中的单词resolve,但是返回的却是Fail 有什么办法我只能得到现场解析吗?请帮忙 谢谢大家!将特定JSON字段从.responseText提取到单个excel单元格,json,excel,vba,api,web-scraping,Json,Excel,Vba,Api,Web Scraping,我试图从JSON中检索一个特定字段resolve。我不知道如何才能得到那一块地。我添加了Msgbox[Exists&Fail]以查看代码是否能够读取单元格中的单词resolve,但是返回的却是Fail 有什么办法我只能得到现场解析吗?请帮忙 谢谢大家! TargetURL = "https://api.passivetotal.org/v2/dns/passive?query=passivetotal.org" actionType = "Content-Type" actio
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
TargetURL=”https://api.passivetotal.org/v2/dns/passive?query=passivetotal.org"
actionType=“内容类型”
actionWord=“应用程序/json”
使用CreateObject(“Microsoft.XMLHTTP”)
.打开“获取”,TargetURL,False
.setRequestHeader操作类型,actionWord
.setRequestHeader“授权”、“基本”
.发送
如果.Status=200,则
表(6).单元格(计数,10).Value=“已连接”
Debug.Print.responseText
MsgBox.responseText
设置JSON=ParseJson(.responseText)
表(6).单元格(计数,8).值=.responseText
如果表(6).单元格(计数,8).值=(“解析”),则
MsgBox(“存在”)
其他的
MsgBox(“失败”)
如果结束
其他的
MsgBox.Status&“:”&.StatusText
如果结束
以
解析JSON响应:
以下内容从文件中读取json结果并解析每个解析。它使用JSONConverter.bas
。注意,我在python脚本中提取了“results”JSON集合,这与您通过Set JSON=JsonConverter.ParseJson(.responseText)(“results”)对转换后的JSON字符串执行JSON(“results”)
操作相同
将JSONConverter.bas
添加到项目后,需要转到工具>参考>添加对Microsoft脚本运行时的参考
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结构如下所示:
返回的对象是词典的集合。在尝试代码时,您可以通过字典键“resolve”
访问所需的值,因为它会引发未经授权的错误。您可以编辑您的帖子,并放置JSONI示例,假设是base64编码的用户+密码字符串?这是否成功地击中了API,而问题仅仅在于访问JSON响应中的项?还是电话失败了?答案对你有用吗?太好了。你太棒了。我认为循环将通过json中每个项目的“results”(“results”)
,项目将被声明为Variant.。这对我来说很有用way@YasserKhalil我在python中使用:pdns_results['results']时拉出了结果集合。所以,我已经在使用必要的json了。但是,是的,使用vba,您希望json(“结果”)生成相同的json。我将编辑以澄清。非常感谢您指出这一点。我对python没有什么好主意。我已经安装了它,但如何测试您提供的代码?您是否安装了带有spyder的anaconda?您还可以通过anaconda/azure机器学习工作室使用jupyter笔记本,或者创建一个.py文件,插入脚本并从pycharm之前安装的python/命令行调用该脚本。可以吗?
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']))