JSON stringify 将 0 转换为空

JSON stringify converts 0 to null

本文关键字:转换 stringify JSON      更新时间:2023-09-26

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