在Meteor中导入JSON文件 [英] Importing a JSON file in Meteor

查看:81
本文介绍了在Meteor中导入JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要加载的data.json文件,并将其放置在lib/文件夹中.为了将JSON加载到服务器中的变量中,我应该怎么做? 谢谢

I have a data.json file that I would like to load and that I have placed in the lib/ folder. What should I do in order to load that JSON into a variable in the server? Thanks

推荐答案

您可以通过三种方式解决此问题,这取决于您对&您的用例.

There are three ways you can go about this, it depends what you're most comfortable with & your use case.

首先是将其存储为JS对象

如果您的json数据为{ "name":"bob" },则可以在/lib文件夹的.js文件中使用myjson = {"name":"bob"},并在需要时调用myjson.

if your json data is { "name":"bob" } you could use myjson = {"name":"bob"} in a .js file in the /lib folder and just call myjson when you need it.

使用http通话

您需要通过meteor add http安装的Meteor http软件包.

You need the Meteor http package, installed via meteor add http.

服务器端代码

myobject = HTTP.get(Meteor.absoluteUrl("/myfile.json")).data;

客户端代码

HTTP.get(Meteor.absoluteUrl("/myfile.json"), function(err,result) }
    console.log(result.data);
});

另一种方法是获取json文件的ajax样式(尽管您必须将其放在/public文件夹中,然后使用Meteor.http进行调用.

Another way to do it is to fetch the json file ajax style (you would have to put it in your /public folder though and use Meteor.http to call it.

直接读取文件

最后,您可以直接读取文件,将myfile.json存储在项目根目录的private目录中:

Lastly you could read the file directly, you store your myfile.json in a private directory in your project's root:

var myjson = {};
myjson = JSON.parse(Assets.getText("myfile.json"));

如果要在客户端访问此文件,则必须将其与Meteor.methods和Meteor.call进行连接

If you want to access this on the client side you would have to interface it with a Meteor.methods and Meteor.call

所以,无论哪种方式,第一种都是最简单的,但我不太确定您要如何使用它,或者您是否要选择文件或其他东西

So whichever way you want, the first is the easiest but I'm not too sure how you want to use it or whether you want to pick the file or something

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

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