json中的数据类型_JSON中的数据类型

json中的数据类型

We know that we use JSON as a mechanism and a medium to transport data on the web. Inside the key-value pairs, the JSON data has a predefined schema which tells us what types of data these values can manifest. In this article, we'll look at some common data types in JSON.

我们知道我们使用JSON作为一种机制和一种在Web上传输数据的媒介。 在键值对内部,JSON数据具有预定义的架构,该架构告诉我们这些值可以显示哪些数据类型。 在本文中,我们将研究JSON中的一些常见数据类型

主要类型 (Primary Types)

JSON data can assume the values to be any of the primary data types such as a string, number, integer, boolean constants or null. These can be in the form of the ordered or unordered list too in the form of objects and arrays but we'll explore them separately later. JSON Documents can be either basic values (strings, numbers, integers, the boolean constants or null). Thus the four basic value types supported by JSON Schema are,

JSON数据可以假定值是任何主要数据类型,例如字符串数字整数布尔常量null 。 它们可以是有序列表或无序列表的形式,也可以是对象和数组的形式,但是我们稍后将分别进行探讨。 JSON文档可以是基本值( 字符串数字整数布尔常量null )。 因此,JSON模式支持的四种基本值类型是:

  • String

  • Integer

    整数

  • Boolean

    布尔型

  • NULL

    空值

JSON字串 (JSON String)

The values in JSON data can be strings or simply a set of characters. We can also specify the entire document to have only the string schema too by writing,

JSON数据中的值可以是字符串,也可以只是一组字符。 我们还可以通过编写将整个文档指定为也只有字符串模式,

    {"type": "string"}

All the characters must be enclosed inside double quotes and should be logically interpreted as a string to have the string schema,

所有字符必须用双引号引起来,并且在逻辑上应解释为具有字符串模式的字符串,

    {"id": "101"}

The above JSON specified id to be a string but does not satisfy the string schema. Similarly,

上面的JSON将id指定为字符串,但不满足字符串架构。 同样,

    {"flag": "true"}

It also specifies to be a string but yet again does not satisfy the string schema.

它还指定为字符串,但又一次不满足字符串模式。

    {"name": "Ryan"}

Satisfied the string schema completely. There are certain restrictions we can impose on the string value. For instance, we specify the minimum and maximum length of our string,

完全满足字符串模式。 我们可以对字符串值施加某些限制。 例如,我们指定字符串的最小和最大长度,

{
  "type": "string", 
  "minLength": 3,
  "maxLength": 7
}

The above schema uses some string restrictions in JSON.

上面的架构在JSON中使用了一些字符串限制。

JSON编号 (JSON Number)

Another form of value in JSON are numbers. For example,

JSON中另一种形式的值是数字。 例如,

    {"age": 45}

In JSON Schema we can also specify that a document must be a number by writing,

在JSON模式中,我们还可以通过以下方式指定文档必须为数字:

    {"type": "number"}

Again as previously in a JSON Schema, the numeric value must only be a number and not a floating-point. However, inside the document, the value can be anything as long as it satisfies any one of the below-mentioned rules,

再次像以前在JSON模式中一样,数字值必须仅是数字,而不是浮点数。 但是,在文档内部,该值可以是任何值,只要它满足以下任一规则,

  1. It is represented in base 10 with no superfluous leading zeros.

    它以10为基数表示,没有多余的前导零。

  2. Digits are between 0 and 9.

    位数在0到9之间。

  3. It's a negative number or a fraction.

    这是一个负数或一个分数。

  4. It's an exponent of 10 prefixed by e.

    这是一个以e开头的10的指数。

{
  "pages" : 210,
  "temperature_kelvin" : -210,
  "temperature_celcius" : 21.05,
  "num" : 1.0E+2
}

The above JSON gives an example of the various kinds of numbers we can have. We can also specify the JSON schema to be an integer,

上面的JSON给出了我们可以拥有的各种数字的示例。 我们还可以将JSON模式指定为整数,

    {"type": "integer"}

We can also have restrictions while using numbers in a JSON schema using maximum and minimum properties.

当在使用最大和最小属性的JSON模式中使用数字时,我们也会受到限制。

JSON布尔值 (JSON Booleans)

When a JSON value is a boolean it is either true or false without any quotes. If we put true and false within quotes it will be treated as a string.

如果JSON值为布尔值,则为true或false,且不带引号。 如果我们在引号中放入对与错,它将被视为一个字符串。

    {"isPrime": true}

We can also specify the type to be Boolean inside a JSON schema,

我们还可以在JSON模式中将类型指定为布尔型,

    {"type": "Boolean"}

JSON空 (JSON Null)

Values in JSON can also be null if they are expected to be empty.

如果JSON中的值应该为空,则也可以为null。

    {"salary": null}

JSON values cannot be a function, date or undefined. We can use these data types to create JSON data, convert JSON data from one type to another and also generate JSON Schemas especially in a Nosequel type of databases like MongoDB, firebase, etc. We'll explore two more data types in JSON- arrays and objects separately.

JSON值不能是函数,日期或未定义。 我们可以使用这些数据类型来创建JSON数据,将JSON数据从一种类型转换为另一种类型,还可以生成JSON模式,尤其是在Nosequel类型的数据库(如MongoDB,firebase等)中。我们将在JSON数组中探索另外两种数据类型。和对象分开。

翻译自: https://www.includehelp.com/json/data-types-in-json.aspx

json中的数据类型

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值