欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

json简介_JSON简介

程序员文章站 2024-01-12 20:15:16
...

json简介

JSON is a file format that’s used to store and interchange data.

JSON是一种用于存储和交换数据的文件格式。

Data is stored in a set of key-value pairs.

数据存储在一组键值对中。

This data is human readable, which makes JSON perfect for manual editing.

这些数据是人类可读的,这使得JSON非常适合手动编辑。

Here’s an example of a JSON string:

这是JSON字符串的示例:

{
  "name": "Flavio",
  "age": 35
}

From this little snippet you can see that keys are wrapped in double quotes, a colon separates the key and the value, and the value can be of different types.

从这个小片段中,您可以看到键被用双引号引起来,冒号将键和值分开,并且值可以是不同的类型。

Key-value sets are separated by a comma.

键值集用逗号分隔。

Spacing (spaces, tabs, new lines) does not matter in a JSON file. The above is equivalent to

JSON文件中的间距(空格,制表符,换行)无关紧要。 以上相当于

{"name": "Flavio","age": 35}

or

要么

{"name":
"Flavio","age":
35}

but as always well-formatted data is better to understand.

但与往常一样,格式更好的数据更易于理解。

JSON was born in 2002 and got hugely popular thanks to its ease of use, and flexibility, and although being born out of the JavaScript world, it quickly spread out to other programming languages.

JSON诞生于2002年,由于其易用性和灵活性而广受欢迎,尽管JSON起源于JavaScript世界,但它Swift传播到其他编程语言。

It’s defined in the ECMA-404 standard.

它在ECMA-404标准中定义。

JSON strings are commonly stored in .json files and transmitted over the network with an application/json MIME type.

JSON字符串通常存储在.json文件中,并通过application/json MIME类型通过网络传输。

资料类型 (Data types)

JSON supports some basic data types:

JSON支持一些基本数据类型:

  • Number: any number that’s not wrapped in quotes

    Number :任何不包含在引号中的数字

  • String: any set of characters wrapped in quotes

    String :用引号引起来的任何字符集

  • Boolean: true or false

    Booleantruefalse

  • Array: a list of values, wrapped in square brackets

    Array :值列表,用方括号括起来

  • Object: a set of key-value pairs, wrapped in curly brackets

    Object :一组括在大括号中的键/值对

  • null: the null word, which represents an empty value

    nullnull单词,表示一个空值

Any other data type must be serialized to a string (and then de-serialized) in order to be stored in JSON.

任何其他数据类型都必须序列化为字符串(然后反序列化)才能存储在JSON中。

用JavaScript编码和解码JSON (Encoding and decoding JSON in JavaScript)

ECMAScript 5 in 2009 introduced the JSON object in the JavaScript standard, which among other things offers the JSON.parse() and JSON.stringify() methods.

ECMAScript 5在2009年引入了JavaScript标准中的JSON对象,该对象除其他外还提供JSON.parse()JSON.stringify()方法。

Before it can be used in a JavaScript program, a JSON in string format must be parsed and transformed in data that JavaScript can use.

在将其用于JavaScript程序之前,必须先解析字符串格式的JSON,然后将其转换为JavaScript可以使用的数据。

JSON.parse() takes a JSON string as its parameter, and returns an object that contains the parsed JSON:

JSON.parse()将JSON字符串作为其参数,并返回一个包含已解析的JSON的对象:

json简介_JSON简介

JSON.stringify() takes a JavaScript object as its parameter, and returns a string that represents it in JSON:

JSON.stringify()将JavaScript对象作为其参数,并返回一个表示JSON的字符串:

json简介_JSON简介

JSON.parse() can also accepts an optional second argument, called the reviver function. You can use that to hook into the parsing and perform any custom operation:

JSON.parse()也可以接受一个可选的第二个参数,称为reviver函数。 您可以使用它来挂钩解析并执行任何自定义操作:

JSON.parse(string, (key, value) => {
  if (key === 'name') {
    return `Name: ${value}`
  } else {
    return value
  }
})

嵌套对象 (Nesting objects)

You can organize data in a JSON file using a nested object:

您可以使用嵌套对象将数据整理到JSON文件中:

{
  "name": {
    "firstName": "Flavio",
    "lastName": "Copes"
  },
  "age": 35,
  "dogs": [
    { "name": "Roger" },
    { "name": "Syd" }
  ],
  "country": {
    "details": {
      "name": "Italy"
    }
  }
}

使用JSON的在线工具 (Online tools for working with JSON)

There are many useful tools you can use.

您可以使用许多有用的工具。

One of them is JSONLint, the JSON Validator. Using it you can verify if a JSON string is valid.

其中之一是JSONLint ,即JSON验证程序。 使用它,您可以验证JSON字符串是否有效。

JSONFormatter is a nice tool to format a JSON string so it’s more readable according to your conventions.

JSONFormatter是格式化JSON字符串的好工具,因此根据您的约定,它更具可读性。

JSON模式 (JSON Schema)

While JSON is very flexible right from the start, there are times when you need a bit more rigid organization to keep things in place.

尽管JSON从一开始就非常灵活,但是有时您需要更严格的组织来保持工作状态。

This is when JSON Schema gets into play. It’s a way to annotate and validate JSON documents according to some specific format you create.

这是JSON Schema发挥作用的时候。 这是一种根据您创建的特定格式注释和验证JSON文档的方法。

翻译自: https://flaviocopes.com/json/

json简介