string형 값을 배열형으로 변환하려면 parse() 메서드를 사용하십시오. 다음은 코드입니다 -
예시
var customerDetails='[
{"name": "John", "countryName": "US"},
{"name": "David", "countryName": "AUS"},
{"name": "Bob", "countryName": "UK"}
]';
console.log("The actual value="+customerDetails);
var convertStringToArray=JSON.parse(customerDetails);
console.log("After converting string to array objects=");
console.log(convertStringToArray); 위의 프로그램을 실행하려면 다음 명령을 사용해야 합니다 -
node fileName.js.
출력
여기에서 내 파일 이름은 demo123.js입니다. 이것은 다음과 같은 출력을 생성합니다 -
PS C:\Users\Amit\JavaScript-code> node demo123.js
The actual value=[{"name": "John", "countryName": "US"}, {"name": "David", "countryName":
"AUS"}, {"name": "Bob", "countryName": "UK"}]
After converting string to array objects=[
{ name: 'John', countryName: 'US' },
{ name: 'David', countryName: 'AUS' },
{ name: 'Bob', countryName: 'UK' }
]