【问题标题】:JSON stringify not completeJSON字符串化未完成
【发布时间】:2016-01-05 03:51:12
【问题描述】:

有人可以解释为什么会这样吗?

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 模块),在 stringily 中发生的情况也是如此。为什么它没有像我想要的那样打印出我的 JSON?

【问题讨论】:

    标签: javascript json stringify


    【解决方案1】:

    替换

    test['id'] = []
    

    test['id'] = {}
    

    解释是数组的 JSON 字符串化仅使用它们的索引属性(即使未定义),它们介于零和 length-1 之间,而不是它们可能具有的任何其他属性,例如名为“10-7”的东西(显然不是数组索引)。

    【讨论】:

    • 谢谢,成功了。你能解释一下我试图在那里做什么吗?为什么会这样呢?
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2016-11-25
    • 2019-01-17
    • 2016-05-15
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多