【问题标题】:Saving JSON as Dictionary将 JSON 保存为字典
【发布时间】:2020-01-21 05:37:50
【问题描述】:

我是 python 新手,我只需要将电子邮件从 API 输出中提取为 JSON 格式。

示例:'email':'brucelee@.....' 目前我将代码附加到列表中,主要是因为我正在努力输出到字典或我可以使用的东西。提前感谢您的帮助!

{'account_locked': None,
 'activated': None,
 'addresses': None,
 'allow_public_key': None,
 'attributes': None,
 'bad_login_attempts': None,
 'company': None,
 'cost_center': None,
 'created': None,
 'department': None,
 'description': None,
 'displayname': None,
 'email': 'adasda@asdasda'}
moto = []
def make_api_call(id):
    api_instance = jcapiv1.SystemusersApi(jcapiv1.ApiClient(configuration))
    id = id
    content_type = 'application/json' # str |  (default to application/json)
    accept = 'application/json' # str |  (default to application/json)
    fields = 'email firstname lastname username ' # str | Use a space seperated string of field parameters to include the data in the response. If omitted, the default list of fields will be returned.  (optional) (default to )
    filter = 'none' # str | A filter to apply to the query. (optional)
    x_org_id = '' # str |  (optional) (default to )
    api_response1 = api_instance.systemusers_get(id, content_type, accept, fields=fields, filter=filter, x_org_id=x_org_id)
    str(api_response1)
    moto.append(api_response1)


for id in all_ids:
    make_api_call(id)

print(moto)

【问题讨论】:

  • 这应该回答你的问题 - stackoverflow.com/questions/19483351/…>
  • 次要问题,但这里的“保存”一词有点奇怪。通常,您可以将保存一词用于您想要将对象序列化为 json 的反向过程,以便可以轻松地将其 保存 到文件中,反向操作称为反序列化
  • 列表有什么问题?如果您只想提取电子邮件,您最终会得到一个电子邮件列表。字典需要一些辅助信息(作为键或值),因此您不会只拥有所需的电子邮件。

标签: python json api dictionary parsing


【解决方案1】:

您只需:

api_response1 = api_instance.systemusers_get(id, content_type, accept, fields=fields, filter=filter, x_org_id=x_org_id)

import json
jsondata = json.loads(api_response1)
email = jsondata['email']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2015-06-15
    • 2018-02-18
    • 2021-08-08
    相关资源
    最近更新 更多