JSON 的格式也可以是陣列

現在才知道…

真的是這樣子

詳細可以參考 MDN 上的說明,但總而言之,array 也是合法的 JSON 格式資料:

1
2
3
4
5
6
7
8
9
10
11
const json = `[
{
"name": "PeaNu",
"age": 23
},
{
"name": "PPB",
"age": 24
}
]`
console.log(JSON.parse(json)) // (2) [{…}, {…}]

或是反其道而行:

1
2
3
4
5
6
7
8
9
10
11
12
const array = [
{
name: 'PeaNu',
age: 23
},
{
name: 'PPB',
age: 24
}
]
console.log(JSON.stringify(array))
// expect output: [{"name":"PeaNu","age":23},{"name":"PPB","age":24}]
mentor-program-day25 套件大總匯
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×