javascript ES5 정리
js 데이터객체, json 표기법
풀스택 개발자
2020. 9. 22. 01:22
js vs json 변수 선언
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
--js object 데이터 객체--
var n = new Boolean(true);
var n = new Number(3);
var s = new String("hello");
var ar = new Array();
--json 표기법--
var n = true;
var n = 3;
var s = "hello";
var ar = [];
</script>
</body>
</html>
js의 객체형식은 코드가 복잡하다
따라서 json방식으로 변수를 선언하는것이 편하다
내부적으로는 json방식으로 변수를 선언해도 모두다 object 데이터 객체 형식으로 변환이 된다
js vs json 객체 생성
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
--js 데이터객체--
var student = new Object();
student.age = 23;
student.name = "홍길동";
--json 표기법--
var student2 = {"age":23 , "name":"홍길동"};
</script>
</body>
</html>
json은 데이터를 구분하기위한 표현 방법이다.
데이터 표현 방법에는 xml, csv json등 여러가지 방식이 있지만 현재에는 json방식이 가장 많이 사용되고 있다.