填充json文件

时间:2016-05-17 00:27:46

标签: json node.js mongodb seeding

我正在尝试使用一些用户数据库集合的假种子数据填充json文件: 这是我如何写入json文件: Generate json file

以下是我在User.json文件中输出的内容: User.json file

但是,如果我在for循环之后调用console.log(people),这就是我得到的结果:

[ { f_name: 'Destin',
    l_name: 'Halvorson',
    username: 'Mustafa.Nitzsche',
    password: 'password' },
  { f_name: 'Giovani',
    l_name: 'Weber',
    username: 'Jodie.Frami25',
    password: 'password' },
  { f_name: 'Zion',
    l_name: 'Hermann',
    username: 'Brain.Deckow',
    password: 'password' },
  { f_name: 'Walker',
    l_name: 'Donnelly',
    username: 'Berniece14',
    password: 'password' },
  { f_name: 'Stanton',
    l_name: 'Rau',
    username: 'Alysha18',
    password: 'password' },
  { f_name: 'Alexandrea',
    l_name: 'Emard',
    username: 'Juvenal9',
    password: 'password' },
  { f_name: 'Elta',
    l_name: 'Bailey',
    username: 'Foster.Mann',
    password: 'password' },
  { f_name: 'Noemie',
    l_name: 'Zboncak',
    username: 'Guillermo45',
    password: 'password' },
  { f_name: 'Elva',
    l_name: 'Weissnat',
    username: 'Willie_Koss',
    password: 'password' },
  { f_name: 'Danny',
    l_name: 'Dickinson',
    username: 'Melyna_Herman87',
    password: 'password' } ]

这顺便就是我想要的

我只是好奇为什么它会回复到每个元素都是一个“对象”?

任何提示或答案都会很棒!

1 个答案:

答案 0 :(得分:2)

使用 JSON.stringify(object, replacer, space);

对于你的情况我会: JSON.stringify(people, null, '\t');

来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify