Để lặp lại mảng JSON, hãy sử dụng JSON.parse ().
Ví dụ
Sau đây là mã -
var apiValues =
[
'{"name": "John", "scores": [78, 89]}',
'{"name": "David", "scores": [58, 98]}',
'{"name": "Bob", "scores": [56, 79]}',
'{"name": "Mike", "scores": [94, 91]}'
];
var parseJSONObject = apiValues.map(obj => JSON.parse(obj));
console.log("The original String : ", apiValues);
console.log("The JSON Objects : ", parseJSONObject); Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo252.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau trên bảng điều khiển -
PS C:\Users\Amit\javascript-code> node demo252.js
The original String : [
'{"name": "John", "scores": [78, 89]}',
'{"name": "David", "scores": [58, 98]}',
'{"name": "Bob", "scores": [56, 79]}',
'{"name": "Mike", "scores": [94, 91]}'
]
The JSON Objects : [
{ name: 'John', scores: [ 78, 89 ] },
{ name: 'David', scores: [ 58, 98 ] },
{ name: 'Bob', scores: [ 56, 79 ] },
{ name: 'Mike', scores: [ 94, 91 ] }
]