热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

IneedtoknowtheJSONfileusageinMeteorJS.FirstwheretheJSONfilestoreinMeteorFolderSt

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.

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

1 个解决方案

#1


11  

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

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

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

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

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

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

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

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

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.

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

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

请注意,文件扩展名(.json)无关紧要,但使用它是常规的。


推荐阅读
author-avatar
手机用户2502884923
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有