자스를 벗어나서 모든 언어에서 데이터를 포장할때 쓰는 포멧

Untitled

<aside> 💡 JavaScript Object Natation (JSON)

</aside>

💡JSON의 표기법

// Object선언 후 바로 값 할당하기
var exam = {"kor":30, "eng":70, "math":80};
console.log(exam.kor + exam.eng);  // 100

// 배열 안에 객체를 넣을수도 있다.
var ar = [3, 4, 5, 6, exam, [7, 8, 9]];
console.log(ar[5][1]);  // 8
console.log(ar[4].math);  // 80

💡중첩된 데이터의 구조도 쉽게 표현할 수 있다.

var notices = [
	{"id":1, "title":"hello json"},
	{"id":2, "title":"hi json"},
	{"id":3, "title":"json is..."}
];

console.log(notices[1]);   // {"id":2, "title":"hi json"}

Untitled

외부에서 JSON파일을 불러올 때에는 객체가 아닌 문자열 형태로 불러와지게 된다.

var notice = ‘{”id”:1, “title”:”hello”}’;
console.log(notice.title);  // undefined

이것을 객체로 인식시키기 위한 방법 2가지가 있다. to be continued

eval();
JSON.parse();