首页 > 解决方案 > 我想知道我可以在一条路线中读取两个 json 文件吗?

问题描述

学生.JSON

{
    "Students" : [
        {"ID" : "1" ,
         "Name" : "janaka",
         "Grade" : "A"
        },
        {"ID" : "2" ,
         "Name" : "Darshana",
         "Grade" : "B"

父.JSON

{
"parents" : [
    {"pID" : "1" ,
     "pName" : "janaka ravindra"
    },
    {"pID" : "2" ,
     "pName" : "Darshana disanayaka"

一个 .JS 文件中有两条路线....

app.get('/students/:id',function(req ,res){
fs.readFile( __dirname + "/" + "Students_details.json", 'utf8',function(err,data){

        //in here i code completly i got ID,Name,Grade according to the requested id. there is know question to me. but Look next route.....
  }
}

这是我的下一条路线

    app.get('/getFullStudentDetails/:id',function(req , res){
        fs.readFile(__dirname + "/" + "parent.json", 'utf8',function(err,data){
        fs.readFile( __dirname + "/" + "Students_details.json", 'utf8',function(err,data){

           var content = JSON.parse(data); //convert to the JSON herevar 
           st_array = content.Students; // get "Student array to new variable

     });
   });
});

标签: javascriptnode.js

解决方案


尝试要求

const json1 = require("parent.json"); 
const json2 =require("Students_details.json");

推荐阅读