Sqlserver如何调用OA api数据结果?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sqlserver如何调用OA api数据结果?相关的知识,希望对你有一定的参考价值。
因公司管理系统为CS结构,数据库为SQLSERVER,OA为云端系统,想请问各位大神如何直接用SQL2005-2012版的,通过存储过程直接同步到本地服务器?如何提取取这个‘签卡’数据,Api说明:https://www.yunzhijia.com/openplatform/resourceCenter/doc#/gitbook-wiki/home/。
网上参考,但未调成功:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO
declare @ServiceUrl as varchar(1000)
set @ServiceUrl = 'http://localhost:52261/Api/Values'
DECLARE @data varchar(max);
set @data=''
Declare @Object as Int
Declare @ResponseText AS varchar(8000) ;
Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'GET',@ServiceUrl,'false'
Exec sp_OAMethod @Object, 'send', NULL, @data --发送数据
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
EXEC sp_OAGetErrorInfo @Object --异常输出
Select @ResponseText
Exec sp_OADestroy @Object
GO
如何构造循环API调用以使JSON结果可用?
我有一个API,我正在对此进行调用,该请求限制了每个请求50个结果以及每分钟180个请求。但是,一个对象总共可能有10到50,000个结果。我需要将生成的JSON构造为可用的格式,或者将其转换为我可以更轻松地访问的东西,例如pandas,我只是不确定哪个是更好的选择。计划是遍历需要数据的每个对象ID。我当前的API调用看起来像这样:
import requests, time
import json, pandas as pd
#object id's to iterate through
# object_id = ['13','16','95','93','77']
object_id = ['13']
#this block finds the count of entries
for obj in object_id:
data = []
info_url = 'https://api.ontraport.com/1/objects/getInfo?objectID='.format(obj)
headers =
'Api-Appid': 'XXXXXXXXX',
'Api-Key': 'XXXXXXXXX'
response = requests.get(info_url, headers=headers).json()
obj_count = int(response['data']['count'])
#this block ensures all entries are pulled as it iterates in groups of 50
start = 1
while start / obj_count <= 1:
url = 'https://api.ontraport.com/1/objects?objectID=&start='.format(obj, start)
headers =
'Api-Appid': 'XXXXXXXXX',
'Api-Key': 'XXXXXXXXX'
response = requests.get(url, headers=headers).json()
data.append(response)
start += 50
time.sleep(0.34)
with open('C:/Desktop/data.txt'.format(obj), 'w') as outfile:
json.dump(data, outfile)
API产生以下结果:
"code": 0,
"data": [
"id":"111",
"date":"1441326063"
,
"id":"132",
"date":"1441526112"
],
"account_id": 0
我只关心“数据”标签内的部分。但是,当我将结果附加在一起时,对于每个请求,它变成一个由JSON对象组成的大型JSON数组,并且在每个对象内,“数据”是另一个数组。是否可以将其构造为仅附加数据结果,然后格式化为熊猫数据框?
我希望这项工作,有关更多信息,请查看List comprehension
import pandas as pd
import json
json_str = """[
"code": 0,
"data": [
"id":"111",
"date":"1441326063"
,
"id":"132",
"date":"1441526112"
],
"account_id": 0
,
"code": 0,
"data": [
"id":"111",
"date":"1441326063"
,
"id":"132",
"date":"1441526112"
],
"account_id": 0
]"""
json_dict = json.loads(json_str)
df = pd.DataFrame([d for y in json_dict for d in y['data']])
如果只有1个数据块,您可以像使用json字典那样简单地保存它
data.append(response['Data'])
以上是关于Sqlserver如何调用OA api数据结果?的主要内容,如果未能解决你的问题,请参考以下文章
PHP如何调用SQLServer2012的存储过程并获取返回结果集及出参?