<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
const data = [{
name : '혼공 파이썬',
price : 10000,
publisher : '한빛미디어'
}, {
name : 'HTML5 web programming',
price : 20000,
publisher : '한빛아카데미'
}]
const json = JSON.stringify(data)
console.log(json)
console.log(JSON.parse(json))
</script>
</head>
<body>
</body>
</html>
### Array of Objects
- The `data` constant is an array containing two objects. Each object represents a book, with properties for the book's name (`name`), price (`price`), and publisher (`publisher`).
- The first object represents a book named "혼공 파이썬" with a price of 10,000 and published by "한빛미디어".
- The second object represents a book named "HTML5 web programming" with a price of 20,000 and published by "한빛아카데미".
### JSON.stringify
- The `JSON.stringify` method is used to convert the `data` array into a JSON string. This method takes an object or array as input and returns a JSON-formatted string.
- This JSON string is stored in the `json` constant and then logged to the console. The output will be a string representation of the array, with all the object properties preserved in JSON format.
### JSON.parse
- The `JSON.parse` method is used to convert the JSON string stored in `json` back into an array of objects.
- This method takes a JSON-formatted string as input and returns the corresponding JavaScript object or array.
- The result of parsing the JSON string is then logged to the console. This will show the original array of objects as it was before the conversion to a JSON string.
### Output
- When this script runs, the first console.log statement will output the JSON string representation of the `data` array. It will look something like this (formatted for readability):
```json
[
{"name":"혼공 파이썬","price":10000,"publisher":"한빛미디어"},
{"name":"HTML5 web programming","price":20000,"publisher":"한빛아카데미"}
]
```
'개념 > 혼자 공부하는 Javascript' 카테고리의 다른 글
method) forEach() (1) | 2025.01.22 |
---|---|
method) getter and setter method (0) | 2025.01.21 |
method) JSON.stringify() method (0) | 2025.01.19 |
method) Lodash_sortBy() method (1) | 2025.01.18 |
method) map() method (0) | 2025.01.17 |