简体   繁体   English

如何在node.js中获取JSON文件?

[英]How to get JSON file in node.js?

In my app I have directory server and sub-directory x and within this directory there is a file called x.js (written using node.js). 在我的应用程序中,我有目录server和子目录x并且在此目录中有一个名为x.js的文件(使用node.js编写)。

In another sub-directory of server there is a y.json file; server另一个子目录中,有一个y.json文件; I want to use its content in x.js . 我想在x.js使用其内容。

It must be very easy, but nothing yet has helped me, 一定很容易,但是还没有任何帮助,

thanks! 谢谢!

Just include it in your js file 只需将其包含在您的js文件中

var myJson = require('path_to_your_json');
// json file contains {"myValue": "resolved"}

console.log(myJson.myValue); // Prints "resolved";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM