1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
//JSON
//JavaScript Object Notation
//1. Object to JSON
//stringfy(obj)
let json = JSON.stringify(true);
console.log(json);
json = JSON.stringify(['apple','banana']);
console.log(json); //["apple","banana"] 배열 처럼 보이게 출력됨
const rabbit = {
name: 'tori',
color: 'white',
size: null,
birthDate: new Date(),
symbol: Symbol("id"), //자바스크립트에만 있는 것은 제외됨
jump: () => {
console.log(`${this.name} can jump!`); //함수는 제외된다
},
};
json = JSON.stringify(rabbit);
console.log(json);
json = JSON.stringify(rabbit, ['name', 'color']); //{"name":"tori","color":"white"}해당하는 값만 제이슨 형식으로 나타난다
console.log(json);
json = JSON.stringify(rabbit, (key, value) => {
console.log(`key: ${key}, value: ${this.value}`); //this를 꼭 써주어야 한다!!!!!!!!
return value; //가장 처음은 raabit의 object를 감싸고 있는 가장 최상위 것이 전달된다
//return key === 'name' ? 'sigcho' : value; //{"name":"sigcho" ... }, 세밀하게 통제하고 싶을 때 callback함수 이용한다
});
console.log(json);
//2. JSON to Object
//parse(json)
console.clear();
json = JSON.stringify(rabbit);
console.log(json); //string형태로 출력
const obj = JSON.parse(json,(key, value) => {
console.log(`key: ${key}, value: ${value}`);
return key === 'birthDate' ? new Date(value) : value; //birthDate를 Date()객체로 만든다
}); //간단하게 전달하고자하는 객체를 넣는다
console.log(obj); //rabbit 객체 출력 {name: "tori", color: "white", size: null, Date: "2020-05-29T13:32:57"}
rabbit.jump();
//obj.jump(); //오류, Object를 JSON으로 만들때 함수는 변환이 안 됐기 때문
console.log(rabbit.birthDate.getDate()); //20
console.log(obj.birthDate); //.getDate()쓰면 오류, birthDate는 string이기 때문에 API 쓸 수 없다
console.log(rabbit.birthDate.getDate()); //위에서 birthDate를 Date()객체로 바꾸었기에 이제는 출력 가능
|
cs |
30줄 this 오류
JSON 유용한 사이트
http://jsondiff.com서버에 요청 했을 때 처음 받아온 데이터와 두번째 받아온 데이터 차이점 알려준다 디버깅시 유용!!
JSON Diff - The semantic JSON compare tool
jsondiff.com
jsonbeautifier.org/서버에서 받아온 json 포맷 망가진 경우 > 포맷 다시 만들어줌
Json Beautifier - Json Formatter | Json Viewer | Json Editor
Online best free JSON Beautifier tool used as JSON editor, Json viewer, Json Validator and Json formatter to display data in a tree view and plain text.
jsonbeautifier.org
jsonparser.org/ json을 object로 쉽게 확인 가능
JSON Parser - Best JSON Formatter | JSON Editor
The best JSON parser online helps you to converts json to a friendly readable. Means you can do json formatter, json beautifier, json viewer, json editor.
jsonParser.org
tools.learningcontainer.com/json-validator/ json 오류 있는지 확인 가능
Best free online JSON Validator
The JSON Validator(JSONLint) helps debugging JSON data by formatting and validating JSON data, JSON String and Json Objects so that it can easily find and read errors by human beings.
tools.learningcontainer.com
'JavaScript' 카테고리의 다른 글
드림코딩 : JS - 12. Promise, Callback 지옥을 Promise로 예쁘게! (0) | 2020.10.20 |
---|---|
드림코딩 : JS - 11. Callback 지옥 (0) | 2020.10.20 |
드림코딩 : JS -9. array API 총정리 (0) | 2020.10.19 |
드림코딩 : JS - 8. array (0) | 2020.10.19 |
드림코딩 : JS -7. object (0) | 2020.10.19 |