JSON解释

JSON (JavaScript Object Notation) is a lightweight method of storing and sharing information: between the browser and a server, within JavaScript itself, and between programming languages.

JSON(JavaScript对象表示法)是一种存储和共享信息的轻量级方法:在浏览器和服务器之间, JavaScript本身内以及编程语言之间。

If you’ve read my previous articles on arrays, nested arrays and objects, you’re already halfway to understanding JSON. Unlike other syntax-heavy formats such as XML, JSON is already (mostly) JavaScript: there’s very little work for your script to parse through, interpret, or translate JSON. As such, it is a “native” data standard in JavaScript, and has largely replaced XML and other implementations.

如果您阅读过我以前关于数组嵌套数组对象的文章 ,那么您已经对JSON有了一半的了解。 与其他语法繁重的格式(例如XML)不同,JSON已经(大部分是)JavaScript:您的脚本几乎无法解析,解释或转换JSON。 因此,它是JavaScript中的“本机”数据标准,并在很大程度上取代了XML和其他实现。

JSON结构 (JSON Structure)

When it is written inside a script, JSON data is simply defined as a variable; specifically, as an object with comma-separated properties and values:

当将其写入脚本内部时,JSON数据被简单地定义为变量 ; 具体来说,作为具有逗号分隔的属性和值的对象:

var starShips = {
    "name": "Executor",
    "class": "Dreadnought",
    "manufacturer": "Kuat Drive Yards",
    "beam": 19000
}

Note that every property and string value is placed inside quotes, with the only exception being numerical values (and boolean values, which are not shown here).

请注意,每个属性和字符串值都放在引号内,唯一的例外是数字值(和布尔值,此处未显示)。

Referencing the properties can be gained via the familiar dot syntax. In the console:

可以通过熟悉的点语法获取引用属性。 在控制台中

console.log(starShips.name);
> "Executor"

Note that the last value never has a comma after it.

请注意,最后一个值后面不能有逗号。

You could add another starship by adding another object:

您可以通过添加另一个对象来添加另一个星际飞船:

var starShips = [{
    "name": "Executor",
    "class": "Dreadnought",
    "manufacturer": "Kuat Drive Yards",
    "beam": 19000
},
{
    "name": "Millennium Falcon",
    "class": "Light Freighter",
    "manufacturer": "Corellian Engineering Corporation",
    "beam": 34.75
}];

The two potential problems with the array-of-objects is that we don’t have an ability to address the individual entries by name; instead, we must address the primary “slots” in the array numerically:

对象数组的两个潜在问题是我们没有能力按名称处理单个条目。 相反,我们必须以数字方式寻址数组中的主要“插槽”:

starShips[1].name
> Millennium Falcon

嵌套JSON (Nested JSON)

If we want two (or more) entries and be able to address them by name, we must nest objects inside the starShip variable:

如果我们要两个(或更多)条目,并能够按名称来解决这些问题,我们必须里面对象starShip变量:

var starShips = {
  "Executor": {
    "class": "Dreadnought",
    "manufacturer": "Kuat Drive Yards",
    "beam": 19000
  },
  "Millennium Falcon": {
    "class": "Light Freighter",
    "manufacturer": "Corellian Engineering Corporation",
    "beam": 34.75
  }
}

Now each ship has become an object unto itself, with properties and values inside it. To get to specific information, we could extend the dot notation:

现在,每艘船已经成为其自身的对象,并且内部具有属性和值。 为了获得特定信息,我们可以扩展点符号:

starShips.Executor.class
> Dreadnought

However, this doesn’t work in the case of the Millennium Falcon, since the ship name contains a space: trying to use a space in dot notation will result in an error. It’s safer, therefore, to use bracket notation:

然而 ,这并不千年隼的情况下工作,因为船舶名称包含空格:试图用圆点分隔的空间会导致错误。 因此,使用方括号表示法更安全:

starShips["Millennium Falcon"]
> Object {class: "Light Freighter", 
    manufacturer: "Corellian Engineering Corporation",
    beam: 34.75}

starShips["Millennium Falcon"]["class"]
> "Light Freighter"

We could take this nesting as deep as needed.

我们可以根据需要深入嵌套。

多得多 (Much More)

At this point, we’re only dealing with JSON as a variable inside an existing script, but it can be used in many other ways:

在这一点上,我们仅将JSON作为现有脚本中的变量处理,但是可以将其用于许多其他方式:

  • loop through and present all the data in the JSON data structure

    遍历并呈现JSON数据结构中的所有数据
  • read the JSON from a file on the same server

    从同一服务器上的文件读取JSON
  • read from an API, to use information provided by another site

    从API读取,以使用其他站点提供的信息

We’ll be looking at these possibilities and more in the very near future.

我们将在不久的将来探讨这些可能性以及更多可能性。

JSON与MySQL (JSON vs MySQL)

As they are both fundamentally ways of storing and retrieving data, it might be tempting to confuse JSON with databases such as MySQL. While the line may blur between the two, there are still important differences:

由于它们从根本上来说都是存储和检索数据的方式,因此很容易将JSON与MySQL等数据库混淆。 尽管两者之间的界限可能会模糊,但仍存在重要差异:

  • JSON files are usually read “all in one”: that is, you must load the entire .json file in order to get or change any piece of information. MySQL does not have this limitation: it acts only on the information you request, and thus tends to be faster for large information caches.

    JSON文件通常是“全部集成”的:也就是说,您必须加载整个.json文件才能获取或更改任何信息。 MySQL没有此限制:它仅对您请求的信息起作用,因此对于大型信息缓存而言,它往往更快。

  • Databases have much more focus on performance and security; JSON lacks these almost entirely.

    数据库更加关注性能和安全性。 JSON几乎完全缺少这些。
  • In general, MySQL has much more powerful tools for finding, gathering and analysing information than JSON.

    通常,MySQL具有比JSON更强大的工具来查找,收集和分析信息。

You might consider the difference between JSON and MySQL as the difference between short-term and long-term memory, or like passing notes in class versus writing a book. JSON tends to be light, temporary and arbitrary; MySQL is long-term, permanent and robust.

您可能会认为JSON和MySQL之间的区别是短期和长期内存之间的区别,或者像在课堂上传递笔记还是写书一样。 JSON倾向于轻量级临时性任意性 ; MySQL是长期的永久的强大的

翻译自: https://thenewcode.com/339/JSON-Explained

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值