【发布时间】:2017-07-07 05:09:59
【问题描述】:
所以我有这本字典“运行”:
[{
'id': 12,
'suite_id': 2,
'name': 'name',
'description': "desc.",
'nice_id': 3,
'joku_id': None,
'onko': False,
'eikai': False,
'tehty': None,
'config': None,
'config_ids': [],
'passed_count': 1,
'blocked_count': 2,
'untested_count': 3,
'retest_count': 4,
'failed_count': 5,
'custom_status1_count': 0,
'custom_status2_count': 0,
'custom_status3_count': 0,
'custom_status4_count': 0,
'custom_status5_count': 0,
'custom_status6_count': 0,
'custom_status7_count': 0,
'projekti_id': 1,
'plan_id': None,
'created_on': 12343214,
'created_by': 11,
'url': 'google.com'
}, {
'id': 16,
'suite_id': 2,
'name': 'namae)',
'description': "desc1",
'nice_id': 5,
'joku_id': None,
'onko': False,
'eikai': False,
'tehty': None,
'config': None,
'config_ids': [],
'passed_count': 100,
'blocked_count': 1,
'untested_count': 3,
'retest_count': 2,
'failed_count': 5,
'custom_status1_count': 0,
'custom_status2_count': 0,
'custom_status3_count': 0,
'custom_status4_count': 0,
'custom_status5_count': 0,
'custom_status6_count': 0,
'custom_status7_count': 0,
'prokti_id': 7,
'plan_id': None,
'created_on': 4321341644,
'created_by': 11,
'url': 'google.com/2' }
有大约 50 次“id”。这只是其中的一部分。 我需要找到所有“id”:s(不是 joku_ids、ncie_ids 等。只有“id”)并为它们制作一个字符串/字典 名称和描述相同
我试过了:
j = json.load(run)
ids = (j["id"])
j = json.load(run)
names = (j["name"])
j = json.load(run)
descriptions = (j["description"])
但它会返回:
AttributeError: 'list' object has no attribute 'read'
我还需要发送一个带有特定 id 的请求,在这种情况下,特定 id 用 o 标记。所以我[o] 请求代码如下:
test = client.send_get('get_tests/1/ ')
所以我需要 id[o] 而不是 1。 我试过了
test = client.send_get('get_tests/' + id[o] + '/ ')
但它会返回:
TypeError: 'int' object is not subscriptable
【问题讨论】:
-
所以你想要
id = [d['id'] for d in runs] -
你期望什么答案,你想要结果是列表还是字典?
标签: python json string parsing dictionary