2021年导入解析的终极方法是什么json
What is the ultimate method for importing and parsing json in 2021
ES6 的新功能主要来自早期 jquery,然后是 fetch,以及最近更多的 require 和更少的模块导入。
无论如何,这是在一个封闭的线程中作为更现代的解决方案呈现的,没有解释实际包括 try catch
Answer for 2021, using ES6 module syntax and async/await
In modern JavaScript, this can be done as a one-liner, without the need to install additional packages:
import { readFile } from 'fs/promises';
let data = JSON.parse(await readFile("filename.json", "utf8"));
Add a try/catch block to handle exceptions as needed.
在我的例子中,文件不是一个模块所以我实际上是用 require 导入 async readFile: const { readFile } = require('fs').promises;
然后我当然想在数据到达时对其进行解析并对其进行处理。只是语法还没有深入。
在以下结构中放置成功函数的位置:
const data = JSON.parse(
await readFile(new URL('./its_data.json', import.meta.url));
);
console.log(data);
您认为处理此问题的最终方法是什么?另外,import.meta.url 和新的 URL() class 实例化有什么用? try catch 会放在哪里?
评论?
所以正如评论中所述,没有最终的方法,你不应该寻找一个。使用适合你的东西。我将展示与您提供的代码最接近的方式,这不是唯一的方式,我不会列出所有方式,因为这将花费一个永恒的时间。
无论如何,首先你不应该使用 URL
如果你还不知道它是什么。您可以在 internet 上阅读它,并在使用它们之前阅读 import.meta.url
。但这在这种情况下不是必需的。 readFile
实际上可以接受一个简单的字符串,它是您要读取的文件的路径。所以使用 readFile('./its_data.json', 'utf8')
其次,没有成功功能,因为这段代码使用了承诺。您可以通过导入正常readFile
来选择退出承诺,然后会有成功功能
const {readFile} = require('fs')
readFile('./its_data.json', 'utf8', (error, data) => {
if(error) { /* handle file reading error */ }
try {
const json = JSON.parse(data)
// do something with data
} catch(e) {
// handle json parsing or your code errors
}
})
如果你想使用 promises(你可能会这样做),那么你可能应该首先在互联网上的某个地方阅读它们的工作方式,然后在你的代码中你会做这样的事情:
const {readFile} = require('fs').promises
readFile('./its_data.json', 'utf8')
.then(data => {
const json = JSON.parse(data)
// Do something with data
})
.catch(error => {
// Handle file reading or json parsing or your code errors
})
您可以在 NodeJS 文档
中阅读有关 readFile
函数的更多信息
ES6 的新功能主要来自早期 jquery,然后是 fetch,以及最近更多的 require 和更少的模块导入。
无论如何,这是在一个封闭的线程中作为更现代的解决方案呈现的,没有解释实际包括 try catch
Answer for 2021, using ES6 module syntax and async/await
In modern JavaScript, this can be done as a one-liner, without the need to install additional packages:
import { readFile } from 'fs/promises';
let data = JSON.parse(await readFile("filename.json", "utf8"));
Add a try/catch block to handle exceptions as needed.
在我的例子中,文件不是一个模块所以我实际上是用 require 导入 async readFile: const { readFile } = require('fs').promises;
然后我当然想在数据到达时对其进行解析并对其进行处理。只是语法还没有深入。
在以下结构中放置成功函数的位置:
const data = JSON.parse(
await readFile(new URL('./its_data.json', import.meta.url));
);
console.log(data);
您认为处理此问题的最终方法是什么?另外,import.meta.url 和新的 URL() class 实例化有什么用? try catch 会放在哪里?
评论?
所以正如评论中所述,没有最终的方法,你不应该寻找一个。使用适合你的东西。我将展示与您提供的代码最接近的方式,这不是唯一的方式,我不会列出所有方式,因为这将花费一个永恒的时间。
无论如何,首先你不应该使用 URL
如果你还不知道它是什么。您可以在 internet 上阅读它,并在使用它们之前阅读 import.meta.url
。但这在这种情况下不是必需的。 readFile
实际上可以接受一个简单的字符串,它是您要读取的文件的路径。所以使用 readFile('./its_data.json', 'utf8')
其次,没有成功功能,因为这段代码使用了承诺。您可以通过导入正常readFile
来选择退出承诺,然后会有成功功能
const {readFile} = require('fs')
readFile('./its_data.json', 'utf8', (error, data) => {
if(error) { /* handle file reading error */ }
try {
const json = JSON.parse(data)
// do something with data
} catch(e) {
// handle json parsing or your code errors
}
})
如果你想使用 promises(你可能会这样做),那么你可能应该首先在互联网上的某个地方阅读它们的工作方式,然后在你的代码中你会做这样的事情:
const {readFile} = require('fs').promises
readFile('./its_data.json', 'utf8')
.then(data => {
const json = JSON.parse(data)
// Do something with data
})
.catch(error => {
// Handle file reading or json parsing or your code errors
})
您可以在 NodeJS 文档
中阅读有关readFile
函数的更多信息