JavaScript基础 - JSON

Introducing JSON from json.org

可接受数据类型:

1
2
3
4
5
6
number
boolean
string
null
array
object

注意要点: 字符串必须用 双引号

API:

1
2
JSON.stringify(value, ?replacer, ?space)
JSON.parse(text, ?reviver)

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'use strict';

let Tom = {
name: '汤姆',
age: 18,
gender: true,
height: 1.70,
grade: null,
'middle-school': '\"IEEE\" Middle School',
skills: ['JavaScript', 'C++', 'Python', 'Rust']
};

var s = JSON.stringify(Tom);
console.log(s);

// 格式化输出
var s = JSON.stringify(Tom, null, ' ');
console.log(s);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{"name":"汤姆","age":18,"gender":true,"height":1.7,"grade":null,"middle-school":"\"IEEE\" Middle School","skills":["JavaScript","C++","Python","Rust"]}

{
"name": "汤姆",
"age": 18,
"gender": true,
"height": 1.7,
"grade": null,
"middle-school": "\"IEEE\" Middle School",
"skills": [
"JavaScript",
"C++",
"Python",
"Rust"
]
}

JavaScript基础 - JSON
http://example.com/2023/01/08/JavaScript基础 - JSON/
作者
Ray
发布于
2023年1月8日
许可协议