[英]Can we import or inlcude a Json file in another Json file in javascript or angular js
In my project I have two Json files.在我的项目中,我有两个 Json 文件。
1) File1.json 1) File1.json
{
"abc" : 123
}
2) File2.json 2) File2.json
import File1.json导入 File1.json
{
"xyz": 567
}
I need a way in javascript or angular js, which would help me achieve a file我需要一种 javascript 或 angular js 的方法,这将帮助我实现文件
File3.json whose content will be something like this File3.json 其内容将是这样的
{
"abc" : 123
},
{
"xyz": 567
}
You could use Node and FileSystem您可以使用节点和文件系统
You will need to change you files contents to:您需要将文件内容更改为:
{
"array": [
{
"abc": 123
}
]
}
And和
{
"array": [
{
"xyz": 567
}
]
}
Then you can do this:然后你可以这样做:
const fs = require('fs'); // Require Filesystem
let files = ['/File1.json', '/File2.json']; // The files you want to merge
let newData = { 'array': [] }; // The new data object.
files.map( ( file ) => { // Loop the files
fs.readFile( file, function read(err, data) { // Read file and pass data and the response
if (err) { // Handle error
throw err;
}
let parsedJson = JSON.parse(data) // Turn data into JSON
parsedJson.array.map( ( object ) => { // Loop the array of objects.
newData.array.push(object); // Push the object into the new structure.
});
});
})
fs.writeFileSync('/File3.json', newData); // Write the new file.
I didn't actually run this but this should work.我实际上并没有运行它,但这应该可以工作。
Also i would suggest you use a Database instead of handling data in JSON files.此外,我建议您使用数据库而不是处理 JSON 文件中的数据。 Checkout Mongo DB it does this all for you :)结帐 Mongo DB 它为您完成这一切:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.