json 데이터 형태 참고

http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/



WHAT IS JSON?

JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner. In a nutshell, it gives us a human-readable collection of data that we can access in a really logical manner.

Storing JSON Data

As a simple example, information about me might be written in JSON as follows:

12345
var jason = {
"age" : "24",
"hometown" : "Missoula, MT",
"gender" : "male"
};
view rawgistfile1.js hosted with ❤ by GitHub

This creates an object that we access using the variable jason. By enclosing the variable’s value in curly braces, we’re indicating that the value is an object. Inside the object, we can declare any number of properties using a "name": "value" pairing, separated by commas. To access the information stored in jason, we can simply refer to the name of the property we need. For instance, to access information about me, we could use the following snippets:

12
document.write('Jason is ' jason.age); // Output: Jason is 24
document.write('Jason is a ' jason.gender); // Output: Jason is a male
view rawgistfile1.js hosted with ❤ by GitHub

Storing JSON Data in Arrays

A slightly more complicated example involves storing two people in one variable. To do this, we enclose multiple objects in square brackets, which signifies an array. For instance, if I needed to include information about myself and my brother in one variable, I might use the following:

12345678910
var family = [{
"name" : "Jason",
"age" : "24",
"gender" : "male"
},
{
"name" : "Kyle",
"age" : "21",
"gender" : "male"
}];
view rawgistfile1.js hosted with ❤ by GitHub

To access this information, we need to access the array index of the person we wish to access. For example, we would use the following snippet to access info stored in family:

12
document.write(family[1].name); // Output: Kyle
document.write(family[0].age); // Output: 24
view rawgistfile1.js hosted with ❤ by GitHub

NOTE: This is beneficial if it will be necessary to loop through stored information, as it lends itself to a for loop with an automatically incrementing value.

Nesting JSON Data

Another way to store multiple people in our variable would be to nest objects. To do this, we would create something similar to the following:

123456789101112
var family = {
"jason" : {
"name" : "Jason Lengstorf",
"age" : "24",
"gender" : "male"
},
"kyle" : {
"name" : "Kyle Lengstorf",
"age" : "21",
"gender" : "male"
}
}
view rawgistfile1.js hosted with ❤ by GitHub

Accessing information in nested objects is a little easier to understand; to access information in the object, we would use the following snippet:

123
document.write(family.jason.name); // Output: Jason Lengstorf
document.write(family.kyle.age); // Output: 21
document.write(family.jason.gender); // Output: male
view rawgistfile1.js hosted with ❤ by GitHub

Nested JSON and arrays can be combined as needed to store as much data as necessary.

+ Recent posts