首页 > 解决方案 > 如何通过javascript更改json对象

问题描述

我正在尝试让用户输入要通过 json 传递的 jira 项目。我目前所拥有的是我通过其他评论发现但没有帮助的内容。

Javascript:

document.getElementById('scYes').onclick = function() {
   var jiraProject = document.getElementById('jiraProject');
   const xhr = new XMLHttpRequest();
   const url = 'https://jira2dev.cerner.com/rest/api/2/issue/';
   xhr.open("POST", url, true);
   xhr.setRequestHeader("Content-Type", "application/json");
   xhr.onReadyStateChange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        var json = JSON.parse(' "fields": { "project": {"key": "TENX" }, "parent":{ "ID": "1","key": "TENX-410"},"summary": "Test Entering Task","description": "TEST","issuetype":{"id": "5"}}');

        for (var i = 0; i < json.length; i++) {
            if (json[i].Id == 1) {
                json[i].parent.key = jiraProject;
                break;
            }
        }

        console.log(json);
        xhr.send();
    }
};

};

我希望能够要求用户输入,并根据输入的内容更改 jira 父键。这应该创建一个 jira 子任务,其父级是用户输入的那个。

标签: javascriptjsonjirajira-rest-api

解决方案


因为你json[i].Id不存在......你有类似的东西json[i].fields.parent.id

你的 jiraProject 必须做同样的模式

你可以在这里测试: https ://www.w3schools.com/js/js_json_parse.asp

注意你的 json 结构;应该是这样的>

'{ "name":"John", "age":30, "city":"New York"}'

推荐阅读