简体   繁体   English

如何通过JavaScript更改JSON对象

[英]How to change a json object via javascript

I am trying to have the user input a jira project to be passed through json. 我试图让用户输入要通过json传递的jira项目。 What I have currently is what I have found through other comments but has not helped. 我目前所拥有的是我通过其他评论发现的,但并没有帮助。

Javascript: 使用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();
    }
};

}; };

I would like to be able to ask for user input, and change the jira parent key based on the entered. 我希望能够要求用户输入,并根据输入的内容更改jira父键。 This should create a jira sub-task with the parent being the one the user had entered. 这应该创建一个jira子任务,其父任务是用户输入的任务。

Because your json[i].Id dont exist... you have something like that json[i].fields.parent.id 因为您的json[i].Id不存在...所以您会有类似的json[i].fields.parent.id

And your jiraProject has to do the same pattern 而且您的jiraProject必须执行相同的模式

You can test here: https://www.w3schools.com/js/js_json_parse.asp 您可以在此处进行测试: https : //www.w3schools.com/js/js_json_parse.asp

Get atention to your json structure; 注意您的json结构; should be something like that > 应该是这样的>

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

您只需要像其他字符串一样将其添加到代码中即可。

'{"name": "John", "age": 30, "country": +"'user input'"+ ,"city":"New York"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM