JSON stringify 将 0 转换为空
JSON stringify converts 0 to null
我正在尝试将我的对象转换为 JSON,但它没有将我的枚举转换为 0。当我打印枚举时,我得到 0,但当我在对象内部使用时,它变为空。如果我使用字符串而不是整数,它可以工作。
(function () {
'use strict';
var ItemStatus = {
'Prepared': 0,
'Ongoing': 1,
'Finished': 2
};
module.exports = ItemStatus;
})();
(function () {
'use strict';
var ItemStatus = require('./itemstatus');
function ItemDetail(detail) {
detail = detail || {};
this.message = detail.message || null;
this.location = detail.location || null;
this.status = detail.status || null;
this.date = detail.date || null;
}
module.exports = ItemDetail;
})();
(function () {
'use strict';
var ItemDetail = require('./itemdetail');
var ItemStatus = require('./itemstatus');
function Item(item) {
item = item || {}
this.name = item.name || null;
this.details = item.details || [];
this.isFinished = item.isFinished || null;
this.finishDate = item.finishDate || null;
}
Item.prototype.addDetail = function(message, location,date,status) {
if (this.isFinished) {
this.isFinished = false;
}
console.log('Status: ' + status); //Prints 0 correctly
var detail = new ItemDetail({
message: message,
location: location,
date:date,
status:status
});
this.details.push(detail);
if (status === ItemStatus.Finished) {
this.isFinished = true;
this.finishDate = date;
}
};
module.exports = Item;
})();
测试失败
var should = require('should');
var Item = require('../lib/models/item');
var ItemDetail = require('../lib/models/itemdetail');
var ItemStatus = require('../lib/models/itemstatus');
describe('Item Detail Test:', function() {
this.enableTimeouts(false);
var myItem = new Item({
name: 'Something',
});
myItem.addDetail('Something happened','NY',1212122,ItemStatus.Prepared);
myItem.addDetail('Another thing','NY',1412122,ItemStatus.Ongoing);
myItem.addDetail('It is done','NY',1212122,ItemStatus.Finished);
it('should print JSON', function() {
myItem.name.should.eql('Something');
console.log(myItem.details[0].status);
myItem.details[0].status.should.eql(ItemStatus.Prepared);
console.log(JSON.stringify(myItem));
});
});
打印时,我的项目显示以下内容
{"name":"Something","details":[{"message":"Something happened","location":"NY","status":null,"date":1212122},{"message":"Another thing","location":"NY","status":1,"date":1412122},{"message":"It is done","location":"NY","status":2,"date":1212122}],"isFinished":true,"finishDate":1212122}
您的问题与 JSON 字符串化无关。
行this.status = detail.status || null;
将0
转换为null
。
因为0
是假的,所以你的this.status
将被设置为null
,以便detail.status
0
。
您可以通过以1
启动 ItemStatus 或不使用 this.status = detail.status || null;
来解决此问题
因此,请使用:
var ItemStatus = {
'Prepared': 1,
'Ongoing': 2,
'Finished': 3
};
或者以这种方式进行测试:
this.status = detail.status;
if( this.status !== 0 && !this.status) {
this.status = null;
}
只需调用:
this.status = detail.status;
因为,如果未定义 detail.status,则它是空的,因此需要|| null
。
相关文章:
- 如何在JavaScript中将字符串转换为函数引用
- 如何在Javascript中将JSon对象转换为数组
- 使用JS将数组转换为json对象
- 如何使用json将对象列表从java转换为javascript
- 偶尔结结巴巴地说“;堆叠的”;translate()上的转换(v4.0.0-alpha40)
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 在Javascript中转换对象数组
- 将数字转换为一定数量的硬币
- 如何配置browserfy与Karma使用转换Stringify为Mustache模板
- JSON.stringify不转换数组
- JavaScript Json.stringify replacer 将值转换为字符串
- 如何在不使用 JSON.stringify 的情况下将 JSON 转换为字符串
- JSON stringify 将 0 转换为空
- PHP,将jQuery对象数组转换为PHP数组,$.post和JSON.stringify
- 使用json.stringify将JS对象转换为json字符串
- 检索原始日期值转换JSON.stringify
- 使用JSON发布.Stringify转换关联数组
- 使用stringify将javascript关联数组转换为json对象,反之亦然
- 如何将javascript对象转换为json而不使用" json .stringify"方法
- 使用JSON.stringify()将JSON对象转换为字符串时出现循环对象值错误