[英]How to Get JSON Values and save them in another file in js
我正在嘗試制作一個電子應用程序。
我想讀取json文件( config, messages
)並將其保存到適當的位置。
問題在於該腳本會將第一個值的json保存在具有名稱的文件中。
腳本會獲取第一個值json並保存它,但是沒有獲取下一個值來獲取json只是將鏈接保存為文件格式。
var allText
var adminconfigjson
var adminconfigsave
var adminmessagesjson
var adminmessagessave
var adminconfigjson = "./assets/json/adminconfig.json"
var adminconfigsave = "./bot/AdminOptions/Config.json"
var adminmessagesjson = "./assets/json/adminmessages.json"
var adminmessagessave = "./bot/AdminOptions/Messages.json"
function create_bot() {
var json = [adminconfigjson, adminmessagesjson]
var newjson = [adminconfig, adminmessages]
var save = [adminconfigsave, adminmessagessave]
var text = ""
var i
for (i = 0; i < json.length; i++) {
console.log(json[i])
readTextFile(json[i])
function readTextFile(file) {
var rawFile = new XMLHttpRequest()
rawFile.open("GET", file, false)
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
for (i = 0; i < newjson.length; i++) {
newjson[i] = rawFile.responseText
console.log(newjson[i])
if (newjson[i] != "") {
for (i = 0; i < save.length; i++) {
var newsave = JSON.stringify(save)
for (i = 0; i < newjson.length; i++) {
if (newsave[i] != "") {
savefile(newsave[i], newjson[i])
console.log(save[i])
}
}
}
}
}
}
}
}
rawFile.send(null)
}
function savefile(filepath, content) {
fs.writeFile(filepath, content, function(err) {
console.log("file path" + filepath)
if (err) {
alert("An error ocurred updating the file" + err.message)
console.log(err)
return
}
alert("The file has been succesfully saved")
})
}
}
}
盡管可以更好地表達代碼和問題,但您的意圖是從源中讀取並將JSON保存到新文件名下的目標中。
我保留了它而未引入異步/等待,因此您可以遵循與最初提供的邏輯相同的邏輯。
xhr
請求的問題在於,由於該應用程序是electron
應用程序,因此您無需提取文件,因為文件是基於該應用程序本地生成的。
如果您希望獲取外部JSON文件(例如api
或外部FS
,則需要包括一個xhr模塊/調用。
const fs = require('fs')
const files = [{
source: "./assets/json/adminconfig.json",
destination: "./bot/AdminOptions/Config.json"
}, {
source: "./assets/json/adminmessages.json"
destination: "./bot/AdminOptions/Messages.json"
}]
// bot reads a config
// and then saves the content of source to destination
const create_bot = () => {
// note: you could make a symbolic link
// between the data directory and the output
// but that's up to you
//
// e.g.
// const filenames = fs.readdir(data_directory).then((error, filenames) => filenames)
// we iterate through each file_object
const filenames = files.forEach(file_object => {
// read the source
fs.readFile(file_object.source, "utf-8")
.then((err, content) => {
if (err) {
console.error(`An error ocurred reading the file: ${err.message}`)
throw new Error(err);
return
}
// write the file
// (destination, content, encoding) => callback
fs.writeFile(file_object.destination, content, "utf-8")
.then((err) => {
if (err) {
console.error(`An error ocurred updating the file: ${err.message}`)
throw new Error(err);
return
}
console.log("success")
}
})
})
}
create_bot();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.