如何在Meteor中使用用户JSON文件?

问题描述:

我需要知道Meteor JS中的JSON文件用法​​。首先,JSON文件存储在Meteor文件夹结构中以及如何使用Meteor JS获取JSON数据,是否使用扩展名.JSON?保存JSON文件。我对此并不了解。你能否给我一些关于JSON文件的建议,或者有没有最好的参考来理解Meteor中的JSON文件。

I need to know the JSON file usage in Meteor JS. First where the JSON file store in Meteor Folder Structure and How to get JSON Data using Meteor JS and is it JSON file save using the extension .JSON?. I didn't get any idea about this. Can you please give me suggestions about JSON files or is there any best references for understanding JSON files in Meteor.

如果你想要要读取 JSON数据,您可以使用 assets api 。您可以在任何项目中执行以下测试:

If you want to read JSON data, you can use the assets api. You can do the following test in any project:

1)创建一个名为 private / test.json 的文件以下内容:

1) Create a file called private/test.json with the following contents:

[{"id":1,"text":"foo"},{"id":2,"text":"bar"}]

2)服务器启动时读取文件内容( server / start.js ):

2) Read the file contents when the server starts (server/start.js):

Meteor.startup(function() {
  console.log(JSON.parse(Assets.getText('test.json')));
});

这里我们使用 getText 来阅读该文件的内容(它假定该文件位于 private 目录中)。然后我们将JSON字符串内容传递给 parse ,它将返回一个对象。

Here we are using getText to read the contents of the file (it assumes the file is located in the private directory). Then we are passing the JSON string contents to parse which will return an object.

请注意文件扩展名( .json)并不重要,但是使用它是常规的。

Note that the file extension (.json) does not matter, however it is conventional to use it.