有人能解释一下为什么会这样吗?
var test = JSON
var date = '10-7'
test['id'] = []
test['id'][date] = [[1,2,3]]
test['id'][date].push([1,1,1])
console.log(test) // Output: { id: [ '10-7': [ [Object], [Object] ] ] }
console.log(JSON.stringify(test)) // Output: {"id":[]}
console.log(test['id'][date][0][0]) // Output: 1
严格地说,当我将JSON保存到一个文件(我使用jsonfile模块)时,也会发生什么。为什么它不像我想的那样打印出我的JSON?
发布于 2015-10-07 18:58:47
替换
test['id'] = []
使用
test['id'] = {}
其解释是,数组的JSON字符串只使用介于零和length-1
之间的索引属性(即使未定义),而不使用任何其他属性,例如名为"10-7“的属性(很明显,它不是数组索引)。
https://stackoverflow.com/questions/33000287
复制相似问题