如何在 Meteor 中使用用户 JSON 文件? [英] How to Use User JSON file in Meteor?

查看:25
本文介绍了如何在 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 数据,你可以使用 资产 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.

这篇关于如何在 Meteor 中使用用户 JSON 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆