javascript - 如何通过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 子任务,其父级是用户输入的那个。
解决方案
因为你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"}'
推荐阅读
- ubuntu-14.04 - Binaries are not executing after AFL installation in qemu mode
- amazon-dynamodb - 使用 OR 条件使用分区键和排序键查询 DynamoDB
- linux - 关于世界可写文件的风险
- azure - 使用提琴手捕获 Azure 块 blob 流量
- hyperledger-fabric - 无法启动业务网络
- ios - 尝试运行应用程序时设备重新启动
- centos - mount - 在 fstab 错误中找不到 /dev/sdc
- excel - VBA复制范围并粘贴到另一个工作表
- python - 使用在views.py中存在的方法中传递的变量检查signals.py中的条件 - django
- activiti - 将外部授权插入camunda