JSON stringify将0转换为null

时间:2016-01-27 11:40:52

标签: javascript node.js

我正在尝试将我的对象转换为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}

2 个答案:

答案 0 :(得分:5)

您的问题与 JSON stringify 无关。

this.status = detail.status || null;行将0转换为null 由于0是假的,this.statusnull设置为detail.status01

您可以使用this.status = detail.status || null;启动 ItemStatus 或不使用var ItemStatus = { 'Prepared': 1, 'Ongoing': 2, 'Finished': 3 }; 来解决此问题

所以要么使用:

this.status = detail.status;
if( this.status !== 0 && !this.status) {
  this.status = null;
}

或者以这种方式进行测试:

style="max-width:100% !important;"

答案 1 :(得分:1)

只需致电:

this.status = detail.status;

因为,如果未定义detail.status,则为null,因此需要|| null