From 2049663acdc80124e51df102c093fccc5f03cf46 Mon Sep 17 00:00:00 2001 From: luojiyin Date: Tue, 8 Mar 2022 08:46:50 +0800 Subject: [PATCH 1/2] fix mardown format --- ...-to-parse-a-json-object-with-javascript.md | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md b/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md index 57e4ed7ef..436cbb770 100644 --- a/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md +++ b/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md @@ -1,11 +1,8 @@ -> - 原文地址:[JSON Stringify Example – How to Parse a JSON Object with JS](https://www.freecodecamp.org/news/json-stringify-example-how-to-parse-a-json-object-with-javascript/) -> - 原文作者:[ - - Kris Koishigawa - - ](https://www.freecodecamp.org/news/author/kris/) -> - 译者: -> - 校对者: +> - 原文地址:[JSON Stringify Example – How to Parse a JSON Object with JS](https://www.freecodecamp.org/news/json-stringify-example-how-to-parse-a-json-object-with-javascript/) +> - 原文作者:[Kris Koishigawa](https://www.freecodecamp.org/news/author/kris/) +> +> - 译者: +> - 校对者: ![JSON Stringify Example – How to Parse a JSON Object with JS](https://www.freecodecamp.org/news/content/images/size/w2000/2021/06/602358380a2838549dcc2554.jpg) @@ -99,7 +96,7 @@ For example, when you write JSON in a separate file like with `jane-profile.json And if you make a request to an API, it'll return something like this: -``` +```json {"name":"Jane Doe","favorite-game":"Stardew Valley","subscriber":false} ``` @@ -255,7 +252,7 @@ index.html You'll see this in the console: -``` +```js Fetch API cannot load file:///jokes.json. URL scheme "file" is not supported ``` @@ -395,7 +392,7 @@ const jokesFile = fs.readFileSync('./jokes.json'); But if you log `jokesFile` to the console now, you'd see something like this: -``` +```plain ``` @@ -478,7 +475,7 @@ console.log("This will run first!"); Which prints the following to the console: -``` +```js This will run first! Chuck Norris's keyboard doesn't have a Ctrl key because nothing controls Chuck Norris. ``` @@ -506,4 +503,4 @@ And that's it! We've covered just about everything you need to know about workin Now get out there and parse or stringify JSON to your heart's content. -Did I miss something? How do you parse JSON in your projects? Let me know over on [Twitter](https://twitter.com/kriskoishigawa). \ No newline at end of file +Did I miss something? How do you parse JSON in your projects? Let me know over on [Twitter](https://twitter.com/kriskoishigawa). From 0080e02d73d11090756fd6984448bd401e614b0a Mon Sep 17 00:00:00 2001 From: luojiyin Date: Tue, 8 Mar 2022 10:03:32 +0800 Subject: [PATCH 2/2] translate finish --- ...-to-parse-a-json-object-with-javascript.md | 186 +++++++++--------- 1 file changed, 93 insertions(+), 93 deletions(-) diff --git a/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md b/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md index 436cbb770..2fa762c49 100644 --- a/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md +++ b/chinese/articles/json-stringify-example-how-to-parse-a-json-object-with-javascript.md @@ -1,24 +1,24 @@ > - 原文地址:[JSON Stringify Example – How to Parse a JSON Object with JS](https://www.freecodecamp.org/news/json-stringify-example-how-to-parse-a-json-object-with-javascript/) > - 原文作者:[Kris Koishigawa](https://www.freecodecamp.org/news/author/kris/) > -> - 译者: +> - 译者:[luojiyin](https://github.com/luojiyin1987) > - 校对者: ![JSON Stringify Example – How to Parse a JSON Object with JS](https://www.freecodecamp.org/news/content/images/size/w2000/2021/06/602358380a2838549dcc2554.jpg) -JSON, or JavaScript Object Notation, is all around us. If you've ever used a web app, there's a very good chance that it used JSON to structure, store, and transmit data between its servers and your device. +JSON,即JavaScript对象符号(JavaScript Object Notation),就在我们身边。如果你曾经使用过一个网络应用,它很有可能使用JSON在其服务器和你的设备之间构建、存储和传输数据。 -In this article, we'll briefly go over the differences between JSON and JavaScript, then jump into different ways to parse JSON with JavaScript in the browser and in Node.js projects. +在这篇文章中,我们将简要介绍JSON和JavaScript的区别,然后跳到在浏览器和Node.js项目中用JavaScript解析JSON的不同方法。 -## Differences between JSON and JavaScript +## JSON和JavaScript之间的区别 -While JSON looks like regular JavaScript, it's better to think of JSON as a data format, similar to a text file. It just so happens that JSON is inspired by JavaScript syntax, which is why they look so similar. +虽然JSON看起来像普通的JavaScript,但最好把JSON看作一种数据格式,类似于文本文件。恰好JSON的灵感来自于JavaScript语法,这就是为什么它们看起来如此相似。 -Let's take a look at JSON objects and JSON arrays and compare them to their JavaScript counterparts. +让我们来看看JSON对象和JSON数组,并将它们与它们的JavaScript对应物进行比较。 -### JSON objects vs JavaScript Object Literals +### JSON对象与JavaScript对象字面意义(Object Literals) -First, here's a JSON object: +首先,这里有一个JSON对象: ```json { @@ -30,11 +30,11 @@ First, here's a JSON object: jane-profile.json -The main difference between a JSON object and a regular JavaScript object – also called an object literal – comes down to the quotation marks. All the keys and string type values in a JSON object have to be wrapped in double quotation marks (`"`). +JSON对象和普通的JavaScript对象(也叫对象字面 object literal)的主要区别在于引号。JSON对象中的所有键和字符串类型的值都必须用双引号(`"`)来包裹。 -JavaScript object literals are a bit more flexible. With object literals, you don't need to wrap keys and strings in double quotation marks. Instead, you could use single quotation marks (`'`), or not use any type of quotation mark for the keys. +JavaScript对象字面意义更灵活一些。对于对象字面意义,你不需要用双引号来包裹键和字符串。相反,你可以使用单引号(`'`),或者不对键值使用任何类型的引号。 -Here's what the code above might look like as a JavaScript object literal: +下面是上面的代码作为一个JavaScript对象字面: ```js const profile = { @@ -44,13 +44,13 @@ const profile = { } ``` -Note that the key `'favorite-game'` is wrapped in single quotes. With object literals, you'll need to wrap keys where the words are separated by dashes (`-`) in quotes. +注意,键"'favorite-game'"是用单引号包裹的。对于对象字面,你需要用引号来包装那些由破折号(`-`)分隔的字的键。 -If you'd like to avoid quotation marks, you could rewrite the key to use camel case (`favoriteGame`) or separate the words with an underscore (`favorite_game`) instead. +如果你想避免引号,你可以重写键,使用骆驼字母大小写(`favoriteGame`),或者用下划线来分隔单词(`favorite_game')。 -### JSON arrays vs JavaScript arrays +### JSON数组 vs JavaScript数组 -JSON arrays work pretty much the same way as arrays in JavaScript, and can contain strings, booleans, numbers, and other JSON objects. For example: +JSON数组的工作方式与JavaScript中的数组基本相同,可以包含字符串、布尔值、数字和其他JSON对象。比如说: ```json [ @@ -69,7 +69,7 @@ JSON arrays work pretty much the same way as arrays in JavaScript, and can conta profiles.json -Here's what that might look like in plain JavaScript: +下面是在普通JavaScript中可能出现的情况: ```js const profiles = [ @@ -86,35 +86,35 @@ const profiles = [ ]; ``` -## JSON as a string +## JSON作为一个字符串 -You might be wondering, if there are JSON objects and arrays, couldn't you use it in your program like a regular JavaScript object literal or array? +你可能会想,如果有JSON对象和数组,你就不能在你的程序中像普通的JavaScript对象字面或数组一样使用它吗? -The reason why you can't do this is that JSON is really just a string. +不能这样做的原因是,JSON实际上只是一个字符串。 -For example, when you write JSON in a separate file like with `jane-profile.json` or `profiles.json` above, that file actually contains text in the form of a JSON object or array, which happens to look like JavaScript. +例如,当你在一个单独的文件中写JSON时,如上面的`jane-profile.json`或`profiles.json`,该文件实际上包含了JSON对象或数组形式的文本,它恰好看起来像JavaScript。 -And if you make a request to an API, it'll return something like this: +如果你向API发出请求,它将返回类似这样的东西: ```json {"name":"Jane Doe","favorite-game":"Stardew Valley","subscriber":false} ``` -Just like with text files, if you want to use JSON in your project, you'll need to parse or change it into something your programming language can understand. For instance, parsing a JSON object in Python will create a dictionary. +就像文本文件一样,如果你想在你的项目中使用JSON,你需要把它解析或改变成你的编程语言可以理解的东西。例如,在Python中解析一个JSON对象将创建一个字典。 -With that understanding, let's look at different ways to parse JSON in JavaScript. +有了这种理解,让我们看看在JavaScript中解析JSON的不同方法。 -## How to parse JSON in the browser +## 如何在浏览器中解析JSON -If you're working with JSON in the browser, you're probably receiving or sending data through an API. +如果你在浏览器中使用JSON,你可能是通过API接收或发送数据。 -Let's take a look at a couple of examples. +让我们看一下几个例子。 -### How to parse JSON with `fetch` +### 如何用`fetch`解析JSON -The easiest way to get data from an API is with `fetch`, which includes the `.json()` method to parse JSON responses into a usable JavaScript object literal or array automagically. +从API获取数据的最简单方法是使用`fetch`,它包括`.json()`方法,将JSON响应自动解析为可用的JavaScript对象字面或数组。 -Here's some code that uses `fetch` to make a `GET` request for a developer-themed joke from the free [Chuck Norris Jokes API](https://api.chucknorris.io/): +下面是一些代码,使用 `fetch` 从对一个对开发者免费的 [Chuck Norris Jokes API](https://api.chucknorris.io/)平台,发出 `GET` 请求: ```js fetch('https://api.chucknorris.io/jokes/random?category=dev') @@ -122,7 +122,7 @@ fetch('https://api.chucknorris.io/jokes/random?category=dev') .then(data => console.log(data)); ``` -If you run that code in the browser, you'll see something like this logged to the console: +如果你在浏览器中运行这段代码,你会看到像这样的东西记录在控制台中: ```js { @@ -136,15 +136,15 @@ If you run that code in the browser, you'll see something like this logged to th } ``` -While that looks like a JSON object, it's really a JavaScript object literal, and you can use it freely in your program. +虽然这看起来像一个JSON对象,但它实际上是一个JavaScript对象字面,你可以在你的程序中自由使用它。 -### How to stringify JSON with `JSON.stringify()` +### 如何用`JSON.stringify()`对JSON进行字符串化处理 -But what if you want to send data to an API? +但是,如果你想把数据发送到API呢? -For instance, say you'd like to send a Chuck Norris joke to the Chuck Norris Jokes API so other people can read it later. +例如,假设你想发送一个 Chuck Norris 的joke 到 Chuck Norris Jokes API,以便其他人以后可以阅读它。 -First, you'd write your joke as a JS object literal: +首先,你要把 joke 写成一个JS对象的字面意思: ```js const newJoke = { @@ -153,9 +153,9 @@ const newJoke = { }; ``` -Then, since you're sending data to an API, you'd need to turn your `newJoke` object literal into a JSON string. +然后,由于你要向API发送数据,你需要把你的`newJoke`对象字面意思变成一个JSON字符串。 -Fortunately, JavaScript includes a super helpful method to do just that – `JSON.stringify()`: +幸运的是,JavaScript包含了一个超级有用的方法来做到这一点-`JSON.stringify()`: ```js const newJoke = { @@ -168,11 +168,11 @@ console.log(JSON.stringify(newJoke)); // {"categories":["dev"],"value":"Chuck No console.log(typeof JSON.stringify(newJoke)); // string ``` -While we're converting an object literal into a JSON string in this example, `JSON.stringify()` also works with arrays. +虽然我们在这个例子中把一个对象的字面意思转换成JSON字符串,但是`JSON.stringify()`也适用于数组。 -Finally, you'd just need to send your JSON stringified joke back to the API with a `POST` request. +最后,你只需要用`POST`请求将你的JSON字符串化的 `joke` 送回给API。 -Note that the Chuck Norris Jokes API doesn't actually have this feature. But if it did, here's what the code might look like: +请注意,Chuck Norris `joke API` 实际上并没有这个功能。但是,如果它有的话,下面是代码可能会出现的情况: ```js const newJoke = { @@ -194,13 +194,13 @@ fetch('https://api.chucknorris.io/jokes/submit', { // fake API endpoint }); ``` -And just like that, you've parsed incoming JSON with `fetch` and used `JSON.stringify()` to convert a JS object literal into a JSON string. +就这样,你用`fetch`解析了传入的JSON,并使用`JSON.stringify()`将JSON对象字面意思转换成JSON字符串。 -### How to work with local JSON files in the browser +### 如何在浏览器中处理本地JSON文件 -Unfortunately, it's not possible (or advisable) to load a local JSON file in the browser. +不幸的是,在浏览器中加载一个本地JSON文件是不可能的(或者说是不可取的)。 -`fetch` will throw an error if you try to load a local file. For example, say you have a JSON file with some jokes: +如果你试图加载一个本地文件,`fetch`将抛出一个错误。例如,假设你有一个JSON文件,里面有一些 `jokes`: ```json [ @@ -227,9 +227,9 @@ Unfortunately, it's not possible (or advisable) to load a local JSON file in the jokes.json -And you want to parse it and create a list of jokes on a simple HTML page. +而你想解析它并在一个简单的HTML页面上创建一个 `jokes` 列表。 -If you create a page with the following and open it in your browser: +如果你用以下内容创建一个页面并在浏览器中打开它: ```html @@ -250,17 +250,17 @@ If you create a page with the following and open it in your browser: index.html -You'll see this in the console: +你会在控制台中看到这个: ```js Fetch API cannot load file:///jokes.json. URL scheme "file" is not supported ``` -By default, browsers don't allow access to local files for security reasons. This is a good thing, and you shouldn't try to work around this behavior. +默认情况下,出于安全考虑,浏览器不允许访问本地文件。这是一件好事,你不应该试图绕过这种行为。 -Instead, the best thing to do is to convert the local JSON file into JavaScript. Fortunately, this is pretty easy since JSON syntax is so similar to JavaScript. +相反,最好的办法是将本地的JSON文件转换成JavaScript。幸运的是,这很容易,因为JSON的语法与JavaScript非常相似。 -All you need to do is create a new file and declare your JSON as a variable: +你所需要做的就是创建一个新的文件并将你的JSON声明为一个变量: ```js const jokes = [ @@ -287,7 +287,7 @@ const jokes = [ jokes.js -And add it to your page as a separate script: +并把它作为一个单独的脚本添加到你的页面上: ```html @@ -305,19 +305,19 @@ And add it to your page as a separate script: ``` -You'll be able to use the `jokes` array freely in your code. +你将能够在你的代码中自由使用`jokes`数组。 -You could also use JavaScript `[modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)` to do the same thing, but that's a bit outside the scope of this article. +你也可以使用 JavaScript [modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) 来做同样的事情,但这有点超出了本文的范围。 -But what if you want to work with local JSON files and have Node.js installed? Let's take a look at how to do that now. +但如果你想处理本地的JSON文件,并安装了Node.js,该怎么办?让我们现在来看看如何做到这一点。 -## How to parse JSON in Node.js +## 如何在Node.js中解析JSON -Node.js is a JavaScript runtime that allows you to run JavaScript outside of the browser. You can read all about [Node.js here](https://www.freecodecamp.org/news/the-definitive-node-js-handbook-6912378afc6e/). +Node.js是一个JavaScript运行时,允许你在浏览器之外运行JavaScript。你可以在这里阅读关于 [Node.js](https://www.freecodecamp.org/news/the-definitive-node-js-handbook-6912378afc6e/) 的所有内容。 -Whether you use Node.js to run code locally on your computer, or to run entire web applications on a server, it's good to know how to work with JSON. +无论你是使用Node.js在你的电脑上本地运行代码,还是在服务器上运行整个网络应用,知道如何处理JSON都是很好的。 -For the following examples, we'll use the same `jokes.json` file: +在下面的例子中,我们将使用同一个`jokes.json`文件: ```json [ @@ -344,17 +344,17 @@ For the following examples, we'll use the same `jokes.json` file: jokes.json -### How to parse a JSON file with `require()` +### 如何用`require()`解析一个JSON文件 -Let's start with the easiest method. +让我们从最简单的方法开始。 -If you have a local JSON file, all you need to do is use `require()` to load it like any other Node.js module: +如果你有一个本地的JSON文件,你需要做的就是使用`require()`来加载它,就像其他Node.js模块。: ```js const jokes = require('./jokes.json'); ``` -The JSON file will be parsed for you automatically and you can start using it in your project: +JSON文件将自动为你解析,你可以开始在你的项目中使用它: ```js const jokes = require('./jokes.json'); @@ -362,54 +362,54 @@ const jokes = require('./jokes.json'); console.log(jokes[0].value); // "Chuck Norris's keyboard doesn't have a Ctrl key because nothing controls Chuck Norris." ``` -Note that this is synchronous, meaning that your program will stop until it parses the entire file before continuing. Really large JSON files can cause your program to slow down, so just be careful with that. +注意,这是同步的,意味着你的程序将停止,直到它解析了整个文件才继续。真正大的JSON文件会导致你的程序变慢,所以要小心处理。 -Also, because parsing JSON this way loads the entire thing into memory, it's better to use this method for static JSON files. If the JSON file changes while your program is running, you won't have access to those changes until you restart your program and parse the updated JSON file. +另外,因为用这种方式解析JSON会将整个文件加载到内存中,所以最好对静态JSON文件使用这种方法。如果JSON文件在你的程序运行时发生了变化,你将无法获得这些变化,直到你重新启动你的程序并解析更新的JSON文件。 -### How to parse a JSON file with `fs.readFileSync(`) and `JSON.parse()` +### 如何用`fs.readFileSync(`)和`JSON.parse()`来解析JSON文件 -This is the more traditional way (for lack of a better term) to parse JSON files in Node.js projects – read the file with `fs` (file system) module, then parse with `JSON.parse()`. +这是在Node.js项目中解析JSON文件的比较传统的方式(因为缺乏更好的术语)--用`fs`(文件系统)模块读取文件,然后用`JSON.parse()`进行解析。 -Let's see how to do this with the `fs.readFileSync()` method. First, add the `fs` module to your project: +让我们看看如何用`fs.readFileSync()`方法做到这一点。首先,将`fs`模块添加到你的项目中: ```js const fs = require('fs'); ``` -Then, create a new variable to store the output of the `jokes.json` file and set it equal to `fs.readFileSync()`: +然后,创建一个新的变量来存储`jokes.json`文件的输出,并设置它等于`fs.readFileSync()`: ```js const fs = require('fs'); const jokesFile = fs.readFileSync(); ``` -`fs.readFileSync()` takes a couple of arguments. The first is the path to the file you want to read: +`fs.readFileSync()`需要几个参数。第一个是你想要读取的文件的路径: ```js const fs = require('fs'); const jokesFile = fs.readFileSync('./jokes.json'); ``` -But if you log `jokesFile` to the console now, you'd see something like this: +但如果你现在把`jokesFile`记录到控制台,你会看到这样的东西: ```plain ``` -That just means that the `fs` module is reading the file, but it doesn't know the encoding or format the file is in. `fs` can be used to load pretty much any file, and not just text-based ones like JSON, so we need to tell it how the file is encoded. +这只是意味着`fs`模块正在读取文件,但它不知道文件的编码或格式。`fs`可以用来加载几乎任何文件,而不仅仅是基于文本的文件,如JSON,所以我们需要告诉它文件的编码方式。 -For text-based files, the encoding is usually `utf8`: +对于基于文本的文件,编码通常为 `utf8`: ```js const fs = require('fs'); const jokesFile = fs.readFileSync('./jokes.json', 'utf8'); ``` -Now if you log `jokesFile` to the console, you'll see the contents of the file. +现在如果你把`jokesFile`记录到控制台,你会看到文件的内容。 -But so far we're just reading the file, and it's still a string. We'll need to use another method to parse `jokesFile` into a usable JavaScript object or array. +但是到目前为止,我们只是在读取文件,而且它还是一个字符串。我们需要使用另一种方法将`jokesFile`解析为可用的JavaScript对象或数组。 -To do that, we'll use `JSON.parse()`: +要做到这一点,我们将使用`JSON.parse()`: ```js const fs = require('fs'); @@ -419,13 +419,13 @@ const jokes = JSON.parse(jokesFile); console.log(jokes[0].value); // "Chuck Norris's keyboard doesn't have a Ctrl key because nothing controls Chuck Norris." ``` -As the name suggests, `JSON.parse()` takes a JSON string and parses it into a JavaScript object literal or array. +顾名思义,`JSON.parse()`接收一个JSON字符串,并将其解析为一个JavaScript对象字面或数组(object literal or array)。 -Like with the `require` method above, `fs.readFileSync()` is a synchronous method, meaning it could cause your program to slow down if it's reading a large file, JSON or otherwise. +和上面的`require'方法一样,`fs.readFileSync()'是一个同步方法,这意味着如果你的程序正在读取一个大文件,不管是JSON还是其他文件,它可能会导致你的程序变慢。 -Also, it only reads the file once and loads it into memory. If the file changes, you'll need to read the file again at some point. To make things easier, you might want to create a simple function to read files. +另外,它只读取一次文件并将其加载到内存中。如果文件有变化,你需要在某个时候再次读取该文件。为了使事情更简单,你可能想创建一个简单的函数来读取文件。 -Here's what that might look like: +以下是可能出现的情况: ```js const fs = require('fs'); @@ -444,11 +444,11 @@ const jokes2 = JSON.parse(jokesFile2); console.log(jokes2[0].value); // "Chuck Norris's keyboard is made up entirely of Cmd keys because Chuck Norris is always in command." ``` -### How to parse JSON with `fs.readFile(`) and `JSON.parse()` +### 如何用`fs.readFile(`)和`JSON.parse()`来解析JSON? -The `fs.readFile()` method is very similar to `fs.readFileSync()`, except that it works asynchronously. This is great if you have a large file to read and you don't want it to hold up the rest of your code. +`fs.readFile()`方法与`fs.readFileSync()`非常相似,只是它是异步工作。如果你有一个大文件要读,而你又不想让它耽误你的其他代码,这就很好。 -Here's a basic example: +下面是一个基本的例子: ```js const fs = require('fs'); @@ -456,9 +456,9 @@ const fs = require('fs'); fs.readFile('./jokes.json', 'utf8'); ``` -So far this looks similar to what we did with `fs.readFileSync()`, except we're not assigning it to a variable like `jokesFile`. Because it's asynchronous, any code after `fs.readFile()` it will run before it's finished reading the file. +到目前为止,这看起来和我们对`fs.readFileSync()`所做的相似,只是我们没有把它分配给一个像`jokesFile`这样的变量。因为它是异步的,在`fs.readFile()`之后的任何代码都会在它完成读取文件之前运行。 -Instead, we'll use a callback function and parse the JSON inside it: +相反,我们将使用一个回调函数并解析其中的JSON: ```js const fs = require('fs'); @@ -473,22 +473,22 @@ fs.readFile('./jokes.json', 'utf8', (err, data) => { console.log("This will run first!"); ``` -Which prints the following to the console: +它在控制台中打印出以下内容: ```js This will run first! Chuck Norris's keyboard doesn't have a Ctrl key because nothing controls Chuck Norris. ``` -Like with `fs.readFileSync()`, `fs.readFile()` loads the file into memory, meaning you'll need to read the file again if it changes. +就像`fs.readFileSync()`一样,`fs.readFile()`将文件加载到内存中,这意味着如果文件有变化,你需要再次读取文件。 -Also, even though `fs.readFile()` is asynchronous, it eventually loads the entire file it's reading into memory. If you have a massive file, it may be better to look into [Node.js streams](https://www.freecodecamp.org/news/node-js-streams-everything-you-need-to-know-c9141306be93/) instead. +另外,尽管`fs.readFile()`是异步的,但它最终会将它所读取的整个文件加载到内存中。如果你有一个巨大的文件,最好是研究一下 [Node.js streams](https://www.freecodecamp.org/news/node-js-streams-everything-you-need-to-know-c9141306be93/)。 -### How to stringify JSON with `JSON.stringify()` in Node.js +### 如何在Node.js中用`JSON.stringify()`对JSON进行字符串化 -Finally, if you're parsing JSON with Node.js, there's a good chance that you'll need to return JSON at some point, maybe as an API response. +最后,如果你用Node.js解析JSON,很有可能在某些时候需要返回JSON,也许是作为一个API响应。 -Luckily, this works the same way as in the browser – just use `JSON.stringify()` to convert JavaScript object literals or arrays into a JSON string: +幸运的是,这与浏览器中的工作方式相同--只需使用`JSON.stringify()`将JavaScript对象字头或数组转换成JSON字符串: ```js const newJoke = { @@ -499,8 +499,8 @@ const newJoke = { console.log(JSON.stringify(newJoke)); // {"categories":["dev"],"value":"Chuck Norris's keyboard is made up entirely of Cmd keys because Chuck Norris is always in command."} ``` -And that's it! We've covered just about everything you need to know about working with JSON in the browser and in Node.js projects. +就这样了! 我们已经涵盖了关于在浏览器和Node.js项目中使用JSON所需要知道的一切。 -Now get out there and parse or stringify JSON to your heart's content. +现在你可以去解析或字符串化JSON,以满足你的需求。 -Did I miss something? How do you parse JSON in your projects? Let me know over on [Twitter](https://twitter.com/kriskoishigawa). +我还有什么遗漏?你是如何在你的项目中解析JSON的?请在 [Twitter](https://twitter.com/kriskoishigawa) 上告诉我。