javascript - 我想知道我可以在一条路线中读取两个 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
});
});
});
- 上面我尝试在一条路径中读取两个 JSON 文件,但我不知道这种方式是对还是错。我也想解释一下。
- 以及这种方式是错误的,我想知道如何正确地做到这一点。
- 在最重要的问题中,如果您愿意,我还提到了数组。
- 很好,我将在这里解释这个过程,我将要做什么......
- 当我运行这条路线“'/students/:id'”时,我收到了“ID、姓名、等级”。这很清楚。但是当我运行“/getFullStudentDetails/:id”这条路线时,我想要“Students.JSON”详细信息(ID、姓名、等级)+“parent.JSON(pName)。
- pName 不想显示,因为所有详细信息都按我输入的 id 显示 (ID==pID)
解决方案
尝试要求:
const json1 = require("parent.json");
const json2 =require("Students_details.json");
推荐阅读
- android - Android Studio apk 分析器显示比 finder 更小
- python-3.x - 用 Beautiful Soup 抓取网页(未捕获所有信息)
- html - 如何在元素之后添加字体真棒图标
- python - Error when parsing dictionary with ast.literal_eval or JSON. Possible bug in writing String? Python
- ruby-on-rails - NoMethodError(JsonWebToken:Module 的未定义方法“编码”):
- java - Why does this simple java program for primes doesn't count 1
- python - 从另一个位置导入文件 - python
- angular - 使用 .Net Core 托管的 Angular 应用程序中的 robots.txt 文件
- ios - 尝试在完成处理程序中更新时,CoreData 获取的对象变为 nil - Swift4
- android - 任务“:app:preDebugAndroidTestBuild”执行失败