我有我在 Python 中解析的 JSON,它看起来像这样
{
"New Slim Testing":{
"name":"New Slim Testing",
"id":"6496",
"type":1,
"fullpath":"\\New Slim Testing",
"children": {
"sf_account":{
"name":"sf_account",
"type":1,
"fullpath":"",
"id":"6516"
},
"sf_case":{
"name":"sf_case",
"type":1,
"fullpath":"",
"id":"6517",
"children": {
"sf_case_delete":{
"name":"sf_case_delete",
"type":1,
"fullpath":"",
"id":"6518"
}
}
},
"sync_incr_sfdc_b2b_to_rds_sf_case":{
"name":"sync_incr_sfdc_b2b_to_rds_sf_case",
"type":2,
"fullpath":"",
"command":"<T4I_DNA_SCRIPTS.23>run_infacloud_task.ksh",
"parameters":"0 <JobName> DSS",
"id":"6520"
}
}
}
}
此 JSON 将用于进行 API 调用。虽然 id 目前是硬编码的,但它应该是动态的,并由 API 调用返回。如何在迭代时更改此字典中的某些值?
例如:父作业将被创建,它将被分配一个 id,当创建子作业时,我将使用它的父 id 等等。
全路径字段也是如此。我想在运行时(迭代字典)派生它,而不是硬编码它。
这是我想要做的
def jsonTraverser(json,parentJson):
templateXML = settings.JOBXMLTEMPLATE
for key, val in json.items():
if isinstance(val, dict):
pp = pprint.PrettyPrinter(indent=2)
if(key!="children"):
parentId = parentJson["id"]
fullpath = '{}\\{}'.format(parentJson["fullpath"],json[key]["name"])
print('Fullpath is {}'.format(fullpath))
json['fullpath'] = fullpath
if val['type']==1:
print('Creating job group {}'.format(val["name"]))
createJob(val,parentId)
else:
print('Creating job {}'.format(val["name"]))
createJob(val,parentId)
jsonTraverser(val,json)
我收到错误消息:字典在迭代期间更改了大小