JSON Jumpstart

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
/**
 * In JSON curly braket is a sign of a new object ({ or })
 * Any variable inside the object, also known as object's attribute, is accessible using a period sign (.) object.attribute
 * or you can also acces it using an "associative array" such as object['attribute']
 * You'll need a Firefox with Firebug extension to see the result from console.log or IE9 with its built-in Developer Tools
 *            
 */


// =========================
// JSON Object
// =========================
var var1 = {
    'var1_1':'value1_1',
    'var1_2':'value1_2'
};
console.log(var1);
// The line above will output> Object { var1_1="value1_1", var1_2="value1_2"}
console.log(var1.var1_1);
// The line above will output> value1_1
console.log(var1['var1_1']);
// The line above will output> value1_1

// =========================
// JSON Array
// =========================
// In JSON square braket is a sign of an array ([ or ])
// Any array element is accessible by using a square bracket and followed by an index (0,1,2,...)
var var2 = ['value2_1', 'value2_2'];
console.log(var2);
// The line above will output> ["value2_1", "value2_2"]
console.log(var2[0]);
// The line above will output> value2_1

var var3 = [{
        'var3_1':'value3_1',
        'var3_2':'value3_2'
    },{
        'var3_3':'value3_3',
        'var3_4':'value3_4'
    }];
console.log(var3);
// The line above will output> [Object { var3_1="value3_1", var3_2="value3_2"}, Object { var3_3="value3_3", var3_4="value3_4"}]
console.log(var3[0].var3_1);
// The line above will output> value3_1
This entry was posted in javascript, Web Development. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


nine − = 0