使用变量构建JSON对象

问题描述 投票:0回答:1

我使用了这个答案Create array of json objects from for loops,并且对于“普通” JSON对象非常有效,但是如果我有一个嵌套元素不能正常工作,这是我的代码:

story_project=open('json_jira/stories/stories_to_jira_TESTEST.json', 'w+'
#######Projects############
json_projects = []
p_name_a, p_key_a, p_type_a = [], [], []
#######Issues##############
json_issues = []
i_summary_a, i_created_a, i_reporter_a, i_status_a, i_issue_type_a = [], [], [], [], []
#######Custom Fields########
json_custom_field_values = []
cf_field_name_a, cf_field_type_a, cf_value_a = [], [], []

#########The Values################
p_name_a.append("ClubHouseDEV")
p_key_a.append("CLUB")
p_type_a.append("software")

i_summary_a.append("This summary doesn not exist")
i_created_a.append("2017-07-17T02:35:16Z")
i_reporter_a.append("5a02285487c3eb1913c44a80")
i_status_a.append("Open")
i_issue_type_a.append("Milestones")

cf_field_name_a.append("external_id")
cf_field_type_a.append("com.atlassian.jira.plugin.system.customfieldtypes:float")
cf_value_a.append(3)

cf_field_name_a.append("Story Points")
cf_field_type_a.append("com.atlassian.jira.plugin.system.customfieldtypes:float")
cf_value_a.append(5)
###########Build The JSON##############

json_custom_field_values = [{"fieldName": cf_field_name, "fieldType": cf_field_type, "value": cf_value} for cf_field_name, cf_field_type, cf_value in zip(cf_field_name_a, cf_field_type_a, cf_value_a)]

json_issues = [{"sumamry": i_summary, "created": i_created, "reporter": i_reporter, "status": i_status, "issueType": i_issue_type, "customFieldValues" : json_custom_field_value} for i_summary, i_created, i_reporter, i_status, i_issue_type, json_custom_field_value   in zip(i_summary_a, i_created_a, i_reporter_a, i_status_a, i_issue_type_a, json_custom_field_values)]

json_projects = [{"name": p_name, "key": p_key, "type": p_type, "issues": json_issue} for p_name, p_key, p_type,json_issue   in zip(p_name_a, p_key_a, p_type_a,json_issues)]

json_file = [{"projects": json_project} for json_project  in zip(json_projects)]
json.dump(json_file, story_project)

输出应为:

{ "projects": [
        {
            "name": "ClubHouseDEV",
            "key": "CLUB",
            "type":"software",
            "issues": 
            [
                {
                    "summary":"This summary doesn not exist",
                    "created":"2017-07-17T02:35:16Z",
                    "reporter":"5a02285487c3eb1913c44a80",
                    "status":"Open",
                    "issueType":"Milestones",
                    "customFieldValues": 
                    [
                        {
                            "fieldName": "external_id",
                            "fieldType": "com.atlassian.jira.plugin.system.customfieldtypes:float",
                            "value": 3
                        },
                        {
                            "fieldName": "Story Points",
                            "fieldType": "com.atlassian.jira.plugin.system.customfieldtypes:float",
                            "value": 5
                        }
                    ],
                    "labels" : ["ch_epics"],
                    "updated": "2017-07-17T02:35:16Z"
                }
            ]

        }
    ]
}

但是它是:

[{"projects": [{"name": "ClubHouseDEV", "key": "CLUB", "type": "software", "issues": {"sumamry": "This summary doesn not exist", "created": "2017-07-17T02:35:16Z", "reporter": "5a02285487c3eb1913c44a80", "status": "Open", "issueType": "Milestones", "customFieldValues": {"fieldName": "external_id", "fieldType": "com.atlassian.jira.plugin.system.customfieldtypes:float", "value": 3}}}]}]

如您所见,它仅在嵌套的“自定义字段值”上添加了一个值,如何添加所有值。

python json python-3.x
1个回答
0
投票

这是我解决id的方法:构建最深的级别,然后将其与级别集成在一起,依此类推,输出是预期的。

© www.soinside.com 2019 - 2024. All rights reserved.