JavaScript Object Notation
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": ["Radiation resistance", "Turning tiny", "Radiation blast"]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
]
}
이 객체를 Javascript 프로그램에서 로드하고, 예를 들어 superHeroes
라는 이름의 변수에 파싱하면 점/브라켓 표현법을 통해 객체 내 데이터에 접근할 수 있게 됩니다.
superHeroes.homeTown; // 'Metro City'
superHeroes["active"]; // true
superHeroes["members"][1]["powers"][2]; // 'Superhuman reflexes'
sometimes we receive a raw JSON string, and we need to convert it to an object ourselves. And when we want to send a JavaScript object across the network, we need to convert it to JSON (a string) before sending it. Luckily, these two problems are so common in web development that a built-in JSON object is available in browsers, which contains the following two methods:
parse()
: Accepts a JSON string as a parameter, and returns the corresponding JavaScript object.stringify()
: Accepts an object as a parameter, and returns the equivalent JSON string.var myJSON = { name: "Chris", age: "38" };
myJSON;
var myString = JSON.stringify(myJSON);
myString;