我有以下格式的JSON
{
"board_title": "test",
"read_only": false,
"isIntegration": false,
"board_bgtype": "board_graph",
"created": "2017-08-16T06:40:47.158868+00:00",
"original_title": "Revised_CID_Templating-test(cloned)",
"modified": "2017-08-31T11:52:22.115661+00:00",
"disableEditing": false,
"height": 111,
"width": "100%",
"template_variables": [
{
"default": "identity",
"prefix": "v1",
"name": "env"
}
],
"created_by": {
"disabled": false,
"handle": "xx.com",
"name": null,
"is_admin": false,
"role": null,
"access_role": "st",
"verified": true,
"email": "xx.com",
"icon": "https://secure.gravatar.com/avatar/86fd6c17deba27cfc4081134a5bc0c6a?s=48&d=retro"
},
...
}
我需要使用Python加载此JSON并编辑
的值"template_variables": [
{
"default": "identity"
对于其他一些价值,请说:
default : "com"
我使用Python脚本加载它并遍历并打印键值对但无法理解如何修改它。
如何为模板变量的第一个子项分配值并将JSON保存在同一个文件中并使用请求发布?
答案 0 :(得分:0)
您可以将json对象视为字典。 试试这个。
import json
jsonData = '''{
"board_title": "test",
"read_only": false,
"isIntegration": false,
"board_bgtype": "board_graph",
"created": "2017-08-16T06:40:47.158868+00:00",
"original_title": "Revised_CID_Templating-test(cloned)",
"modified": "2017-08-31T11:52:22.115661+00:00",
"disableEditing": false,
"height": 111,
"width": "100%",
"template_variables": [
{
"default": "identity",
"prefix": "v1",
"name": "env"
}
],
"created_by": {
"disabled": false,
"handle": "xx.com",
"name": null,
"is_admin": false,
"role": null,
"access_role": "st",
"verified": true,
"email": "xx.com",
"icon": "https://secure.gravatar.com/avatar/86fd6c17deba27cfc4081134a5bc0c6a?s=48&d=retro"
}
}'''
jsonToPython = json.loads(jsonData)
print (jsonToPython['template_variables'][0]['default'])
jsonToPython['template_variables'][0]['default'] = 'test'
print (jsonToPython['template_variables'][0]['default'])
你可以看到正在修改jsonToPython。