首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要从站点获取或转换数据才能成为有效的JSON

需要从站点获取或转换数据才能成为有效的JSON
EN

Stack Overflow用户
提问于 2018-06-01 15:04:32
回答 1查看 66关注 0票数 1

我正在工作的脚本,将从一个网站(思科补丁网站)的数据,并根据收到的数据,我需要张贴到另一个网站(ServiceNow事件管理)。POST需要是REST/JSON,具有特定的键才能工作。

我有足够的代码来获取数据,我有足够的代码来发布工作。

我很难将从get获得的数据转换为有效的JSON键值对来发布。

我正在使用以下代码从思科网站获得一个新的补丁列表。我正在获取正确的数据,但是如果数据不是我如何使用它以JSON格式发布到另一个工具的格式(使用不同的键,而是从返回的信息中获得值)。

这很管用-

代码语言:javascript
复制
def getjson(ciscourl):
    response = urllib.request.urlopen(ciscourl)
    ciscodata = response.read().decode("utf-8")
    return json.loads(ciscodata)

我得到的数据如下所示(这个查询产生了2个补丁):

[{"identifier":"cisco-sa-20180521-cpusidechannel","title":"CPU Side-Channel Information Disclosure Vulnerabilities: May 2018","version":"1.5","firstPublished":"2018-05-22T01:00:00.000+0000","lastPublished":"2018-05-31T20:44:16.123+0000","workflowStatus":null,"id":1,"name":"Cisco Security Advisory","url":"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20180521-cpusidechannel","severity":"Medium","workarounds":"No","cwe":null,"cve":"CVE-2018-3639,CVE-2018-3640","ciscoBugId":"","status":"Updated","summary":"On May 21, 2018, researchers disclosed two vulnerabilities that take advantage of the implementation of speculative execution of instructions on many modern microprocessor architectures to perform side-channel information disclosure attacks. These vulnerabilities could allow an unprivileged, ","totalCount":6,"relatedResource":[]},{"identifier":"cisco-sa-20180516-firepwr-pb","title":"Cisco Firepower Threat Defense Software Policy Bypass Vulnerability","version":"1.0","firstPublished":"2018-05-16T16:00:00.000+0000","lastPublished":"2018-05-16T16:00:00.000+0000","workflowStatus":null,"id":1,"name":"Cisco Security Advisory","url":"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20180516-firepwr-pb","severity":"Medium","workarounds":"No","cwe":"CWE-693","cve":"CVE-2018-0297","ciscoBugId":"CSCvg09316","status":"New","summary":"A vulnerability in the detection engine of Cisco Firepower Threat Defense software could allow an unauthenticated, remote attacker to bypass a configured Secure Sockets Layer (SSL) Access Control (AC) policy to block SSL traffic.The vulnerability is due to the incorrect handling ","totalCount":6,"relatedResource":[]}]

我可以从中提取值,比如print(jarray.get('identifier')),但是我很难用我定义的键将这些值映射到自己的JSON映射中。因此,我返回的键identifier的值需要映射到我的JSON映射中的一个名为"node"的键。

我试过json.loadsjson.loadjson.dumpjson.dumps。每次错误都是属性类型错误。

,这是我困惑的代码:

代码语言:javascript
复制
def createJson(l):
#try:

    jsonarray = l
    o_source = "CiscoUpdatePatchChecker"
    o_node = (jsonarray.get('identifier')) #this does not work
    o_metric_name = ("Critical")
    o_type = ("test")
    o_resource = ("test_resource")
    o_description = jsonarray  #this works
    o_event_class = ("test event class")
    o_additional_info = jsonarray
    print ("-" * 50)
    print (o_source, o_node, o_metric_name, o_type, o_resource, o_description, o_event_class, o_additional_info)
    print ("-" * 50)
    data = {"source": o_source, "node": o_node, "metric_name": o_metric_name, "type": o_type, "resource": o_resource, "event_class": o_event_class, "description": o_description, "additional_info": o_additional_info}
    return json.dumps(data)
# except:
    #pass

除此之外,其余的代码只是将数据发布到正在工作的ITSM。-

代码语言:javascript
复制
def postjson(data):
    # try:
    url = posturl
    auth = HTTPBasicAuth(username, password)
    head = {'Content-type': 'application/json',
            'Accept': 'application/json'}
    payld = data
    ret = requests.post(url, auth=auth , data=payld, headers=head)
    # sys.stdout.write(ret.text)
    returned_data = ret.json()
    print(returned_data)

因此,我的问题是将数据映射回我的键: JSON映射中的值对,我将需要在检索到的补丁数的同时循环代码一次。我目前正计划在我的主要函数中循环需要发布的JSON映射的数量。

现在,我只需要把我得到的所有数据映射到"description""additional_info"字段。这是工作的,我可以张贴的数据罚款。

如果有人能告诉我如何操作从GET请求中获得的数据,这将极大地帮助我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-01 15:26:41

json.loads(ciscodata)返回一个字典数组。

o_node = (jsonarray.get('identifier'))语句失败,因为它试图在jsonarray是列表时获取字典键。

试试o_node = jsonarray[0].get('identifier')

我不知道你为什么会在jsonarray周围有父母--他们不应该在那里。

一般来说,您可以这样做:

代码语言:javascript
复制
for elem in jsonarray:
    identifier = elem.get('identifier')
    title = elem.get('title')
    ... etc
    do_something(identifier, title, ...)

这使得调试变得更容易,因为您可以测试从api调用中得到的值。

如果您总是在字典中获得相同的键,则可以跳过此作业,只需执行以下操作。

代码语言:javascript
复制
for elem in jsonarray:
    do_something(elem.get('key1'), elem.get('key2'), ...)`

希望这有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50646321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档