无法将键添加到 JSON 对象。没有抛出错误
Can't add key to JSON object. No errors thrown
当我运行以下时,我得到
$ node t.js
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
并且 time
key/value 未添加。
有人能知道为什么吗?
t.js
const toml = require('./toml');
const moment = require('moment');
const t = toml('non-production.toml');
let a = new Object;
a = t.Jira.pack.c;
console.log(a);
const time = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");
a["customfield_11904"] = time;
a.customfield_11904 = time;
console.log(a);
t.Jira.pack.c.customfield_11904 = time;
console.log(t.Jira.pack.c);
toml.js
const TOML = require('@iarna/toml');
const fs = require('fs');
module.exports = (filename) => {
return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}
非production.toml
[Jira]
pack.c = '{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}'
在你的例子中,a
仍然是一个字符串。因此,您不能向其添加新密钥。
先解析为JSON:
const toml = require('./toml');
const moment = require('moment');
const t = toml('non-production.toml');
let a = JSON.parse(t.Jira.pack.c);
console.log(a);
const time = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");
a.customfield_11904 = time;
console.log(a);
这现在产生
{
project: { key: 'DEMO' },
issuetype: { id: 10002 },
priority: { id: '3' }
}
{
project: { key: 'DEMO' },
issuetype: { id: 10002 },
priority: { id: '3' },
customfield_11904: '2021-03-12T09:39:15.259+0100'
}
当我运行以下时,我得到
$ node t.js
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
并且 time
key/value 未添加。
有人能知道为什么吗?
t.js
const toml = require('./toml');
const moment = require('moment');
const t = toml('non-production.toml');
let a = new Object;
a = t.Jira.pack.c;
console.log(a);
const time = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");
a["customfield_11904"] = time;
a.customfield_11904 = time;
console.log(a);
t.Jira.pack.c.customfield_11904 = time;
console.log(t.Jira.pack.c);
toml.js
const TOML = require('@iarna/toml');
const fs = require('fs');
module.exports = (filename) => {
return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}
非production.toml
[Jira]
pack.c = '{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}'
在你的例子中,a
仍然是一个字符串。因此,您不能向其添加新密钥。
先解析为JSON:
const toml = require('./toml');
const moment = require('moment');
const t = toml('non-production.toml');
let a = JSON.parse(t.Jira.pack.c);
console.log(a);
const time = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");
a.customfield_11904 = time;
console.log(a);
这现在产生
{
project: { key: 'DEMO' },
issuetype: { id: 10002 },
priority: { id: '3' }
}
{
project: { key: 'DEMO' },
issuetype: { id: 10002 },
priority: { id: '3' },
customfield_11904: '2021-03-12T09:39:15.259+0100'
}