【问题标题】:JSON property value without quote不带引号的 JSON 属性值
【发布时间】:2020-08-16 20:26:31
【问题描述】:

愿那些遵循指导的人平安,

是否有理由不引用全部财产?在通过 axios 帖子使用 JSON.stringify(this.items) 时。

代码:

 items: [
                    { description: 'Item name', quantity: 0, price: 0, discount: 0,  get total() {
                            return this.quantity * this.price;
                        }
                        },
                    { description: 'Item name', quantity: 0, price: 0, discount: 0 ,  get total() {
                            return this.quantity * this.price;
                        }
                    }

                ],

输出

"{"description":"Item name","quantity":"10","price":"100","total":1000,"discount":"1"},
{"description":"Item name","quantity":"10","price":"100","total":1000,"discount":"10"}"

【问题讨论】:

    标签: javascript json vue.js


    【解决方案1】:

    在 JSON 中,值周围的双引号 " 表示 字符串。非字符串不使用"s 分隔。数字没有分隔符,total 属性 getter 在调用时(由JSON.stringify)返回一个数字,因此生成的 JSON 不会用引号将 total 值括起来。

    【讨论】:

    • 是否有通过 js 或 vue 的简单修复来获得引用?
    • 如果你想要引号,请从 getter 中返回一个字符串
    • 第一次调用而不是第二次从 getter 获取引用值有什么原因吗?
    • 什么意思?无论您是对一个对象调用一次还是多次JSON.stringify,它每次都应该返回相同的东西(除了在极少数情况下,getter 改变了一个属性,这不会在这里发生)
    • 我测试了第三次,没有引用第二次!我正在使用 wordpress 中的 admin-ajax.php。
    猜你喜欢
    • 2012-10-14
    • 2014-01-20
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多