1 js/05 data types/11 json#79
Merged
leviding merged 12 commits intojavascript-tutorial:zh-hansfrom May 7, 2018
Merged
Conversation
Contributor
|
@leviding 校对认领 |
Member
|
@Starriers ok |
Starriers
reviewed
May 5, 2018
| @@ -1,10 +1,10 @@ | |||
| # JSON methods, toJSON | |||
| # JSON 方法和 toJSON | |||
| 当然,这样的字符串应该包含所有重要的属性。 | ||
|
|
||
| We could implement the conversion like this: | ||
| 我们可以像这样实现转换: |
|
|
||
| ...But in the process of development, new properties are added, old properties are renamed and removed. Updating such `toString` every time can become a pain. We could try to loop over properties in it, but what if the object is complex and has nested objects in properties? We'd need to implement their conversion as well. And, if we're sending the object over a network, then we also need to supply the code to "read" our object on the receiving side. | ||
| ...但在开发过程中,新增了一些属性,旧的属性被重命名并删除。每次更新这种 `toString` 都会变得很痛苦。 我们可以尝试循环其中的属性, | ||
| 但是如果对象很复杂,并且在属性中嵌套对象呢?我们也需要对他们进行转换。如,如果我们通过网络发送对象,那么我们还需要提供代码来在接收端“读取”我们的对象。 |
|
|
||
| ...But in the process of development, new properties are added, old properties are renamed and removed. Updating such `toString` every time can become a pain. We could try to loop over properties in it, but what if the object is complex and has nested objects in properties? We'd need to implement their conversion as well. And, if we're sending the object over a network, then we also need to supply the code to "read" our object on the receiving side. | ||
| ...但在开发过程中,新增了一些属性,旧的属性被重命名并删除。每次更新这种 `toString` 都会变得很痛苦。 我们可以尝试循环其中的属性, | ||
| 但是如果对象很复杂,并且在属性中嵌套对象呢?我们也需要对他们进行转换。如,如果我们通过网络发送对象,那么我们还需要提供代码来在接收端“读取”我们的对象。 |
| ## JSON.stringify | ||
|
|
||
| The [JSON](http://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) is a general format to represent values and objects. It is described as in [RFC 4627](http://tools.ietf.org/html/rfc4627) standard. Initially it was made for JavaScript, but many other languages have libraries to handle it as well. So it's easy to use JSON for data exchange when the client uses JavaScript and the server is written on Ruby/PHP/Java/Whatever. | ||
| [JSON](http://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) 是表示值和对象的通用格式。它被描述为 [RFC 4627](http://tools.ietf.org/html/rfc4627)标准。最初它是为 JavaScript 编写的,但许多其他语言也有库来处理它。因此,当客户端使用 JavaScript 并且服务器使用 Ruby/PHP/Java/Whatever 编写时,使用 JSON 进行数据交换非常容易。 |
| ``` | ||
|
|
||
| ...And now we need to *deserialize* it, to turn back into JavaScript object. | ||
| ...现在我们需要 **反序列化它**,重新转换成 JavaScript 对象。 |
| `meetup.date` 的值是一个字符串,而不是 `Date` 对象。 `JSON.parse` 如何知道它应该将该字符串转换为 `Date`? | ||
|
|
||
| Let's pass to `JSON.parse` the reviving function that returns all values "as is", but `date` will become a `Date`: | ||
| 让我们传递返回所有值的函数给 `JSON.parse`,但 `date` 将变成了 `Date`,正常运行: |
| - If an object has `toJSON`, then it is called by `JSON.stringify`. | ||
| - JSON 是一种数据格式,对于大多数编程语言都有自己的独立标准和库。 | ||
| - JSON 支持 objects,arrays,strings,numbers,booleans 和 `null`。 | ||
| - JavaScript 提供序列化成 JSON 方法 [JSON.stringify](mdn:js/JSON/stringify)和解析 JSON 方法 [JSON.parse](mdn:js/JSON/parse)。 |
| ...现在我们需要 **反序列化它**,重新转换成 JavaScript 对象。 | ||
|
|
||
| Let's do it by calling `JSON.parse`: | ||
| 让我们通过调用 `JSON.parse` 来完成: |
| ## JSON.parse | ||
|
|
||
| To decode a JSON-string, we need another method named [JSON.parse](mdn:js/JSON/parse). | ||
| 要解码 JSON 字符串,我们需要另一个方法 [JSON.parse](mdn:js/JSON/parse). |
|
@leviding 认领校对 |
Member
|
@jasonxia23 ok |
jasonxia23
reviewed
May 5, 2018
| ``` | ||
|
|
||
| Here we also need to test `key==""` to exclude the first call where it is normal that `value` is `meetup`. | ||
| 这里我们也需要测试 `key==""` 在正常情况下排除第一个调用 `value` 是 `meetup`。 |
There was a problem hiding this comment.
这里我们也需要测试 key=="" 在正常情况下排除第一个调用 value 是 meetup。
=>
这里我们也需要判断 key=="" 以排除第一个调用时 value 是 meetup 的情况。
| @@ -1,10 +1,10 @@ | |||
| # JSON methods, toJSON | |||
| # JSON 方法和 toJSON | |||
| ``` | ||
|
|
||
| ...But in the process of development, new properties are added, old properties are renamed and removed. Updating such `toString` every time can become a pain. We could try to loop over properties in it, but what if the object is complex and has nested objects in properties? We'd need to implement their conversion as well. And, if we're sending the object over a network, then we also need to supply the code to "read" our object on the receiving side. | ||
| ...但在开发过程中,新增了一些属性,旧的属性被重命名并删除。每次更新这种 `toString` 都会变得很痛苦。 我们可以尝试循环其中的属性, |
| ``` | ||
|
|
||
| ...But in the process of development, new properties are added, old properties are renamed and removed. Updating such `toString` every time can become a pain. We could try to loop over properties in it, but what if the object is complex and has nested objects in properties? We'd need to implement their conversion as well. And, if we're sending the object over a network, then we also need to supply the code to "read" our object on the receiving side. | ||
| ...但在开发过程中,新增了一些属性,旧的属性被重命名并删除。每次更新这种 `toString` 都会变得很痛苦。 我们可以尝试循环其中的属性, |
| ## JSON.stringify | ||
|
|
||
| The [JSON](http://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) is a general format to represent values and objects. It is described as in [RFC 4627](http://tools.ietf.org/html/rfc4627) standard. Initially it was made for JavaScript, but many other languages have libraries to handle it as well. So it's easy to use JSON for data exchange when the client uses JavaScript and the server is written on Ruby/PHP/Java/Whatever. | ||
| [JSON](http://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) 是表示值和对象的通用格式。它被描述为 [RFC 4627](http://tools.ietf.org/html/rfc4627)标准。最初它是为 JavaScript 编写的,但许多其他语言也有库来处理它。因此,当客户端使用 JavaScript 并且服务器使用 Ruby/PHP/Java/Whatever 编写时,使用 JSON 进行数据交换非常容易。 |
There was a problem hiding this comment.
并且服务器使用
=>
而服务器使用
这里的并列语气没有那么强烈,用而感觉更顺口
| 第一个调用很特别。它是使用特殊的“包装对象”制作的: `{"": meetup}`。换句话说,第一个 `(key,value)` 对是空键,并且该值是整个目标对象。这就是为什么上面的例子中第一行是 `":[object Object]"` 的原因。 | ||
|
|
||
| The idea is to provide as much power for `replacer` as possible: it has a chance to analyze and replace/skip the whole object if necessary. | ||
| 基于这个理念为 `replacer` 提供了更强大的功能: 如有必要,它有机会分析和替换/跳过整个对象。 |
| 在这里我们可以看到 `date` `(1)` 变成了一个字符串。这是因为所有日期都有一个内置的 `toJSON` 方法来返回这种类型的字符串。 | ||
|
|
||
| Now let's add a custom `toJSON` for our object `room`: | ||
| 现在让我们为对象 `room` 添加一个自定义的 `toJSON`: |
| ## JSON.parse | ||
|
|
||
| To decode a JSON-string, we need another method named [JSON.parse](mdn:js/JSON/parse). | ||
| 要解码 JSON 字符串,我们需要另一个方法 [JSON.parse](mdn:js/JSON/parse). |
| 此外,JSON 不支持注释。向 JSON 添加注释无效。 | ||
|
|
||
| There's another format named [JSON5](http://json5.org/), which allows unquoted keys, comments etc. But this is a standalone library, not in the specification of the language. | ||
| 还有另一种名为 [JSON5](http://json5.org/)的格式,它允许未加引号的键,注释等。但这是一个独立的库,不在该语言的规范中。 |
| 哇!报错了! | ||
|
|
||
| The value of `meetup.date` is a string, not a `Date` object. How could `JSON.parse` know that it should transform that string into a `Date`? | ||
| `meetup.date` 的值是一个字符串,而不是 `Date` 对象。 `JSON.parse` 如何知道它应该将该字符串转换为 `Date`? |
Member
|
@sunhaokk 可以修改啦 |
Contributor
Author
|
@Starriers @jasonxia23 感谢指导 |
leviding
approved these changes
May 7, 2018
Member
|
@sunhaokk 对应文件夹有五个 MarkDown 文件,还有几个需要继续翻一下 |
Contributor
Author
|
@leviding |
Member
|
@sunhaokk importance 不翻译是指这个字段不翻译,这一句保留不翻译。但是文件的其他内容需要翻译。请把两个 task 文件翻译提交上来。 |
leviding
approved these changes
May 7, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
resolve #67
@leviding 翻译完毕