File tree 5 files changed +97
-52
lines changed
5 files changed +97
-52
lines changed Original file line number Diff line number Diff line change 2
2
< html lang ="en ">
3
3
< head >
4
4
< meta charset ="utf-8 ">
5
- < title > Stringifying a JavaScript object to JSON</ title >
5
+ < title > Declaring and using JSON variables </ title >
6
6
< style >
7
7
body {
8
8
margin : 1em ;
9
9
font-size : 1.5em ;
10
10
}
11
- pre {
11
+ div {
12
12
margin-top : .5em ;
13
13
}
14
14
</ style >
15
15
</ head >
16
16
< body >
17
- < pre id ="output ">
18
- </ pre >
17
+ < div id ="output ">
18
+ </ div >
19
19
20
20
< script >
21
+
21
22
// Declare a JavaScript object
22
- const userData = {
23
- bgColor : "darkolivegreen" ,
24
- textColor : "antiquewhite" ,
25
- textSize : "1.25em" ,
26
- typefaces : [ "Georgia" , "Verdana" , "serif" ] ,
27
- subscriber : true ,
28
- subscriptionType : 3
23
+ const customer = {
24
+ "account" : 853 ,
25
+ "name" : "Alfreds Futterkiste" ,
26
+ "supplier" : false ,
27
+ "recentOrders" : [ 28394 , 29539 , 30014 ] ,
28
+ "contact" : {
29
+ "name" : "Maria Anders" ,
30
+ "phone" : "030-0074321" ,
31
+
32
+ }
29
33
}
30
-
31
- // Stringify it
32
- const userDataJSON = JSON . stringify ( userData , null , " " ) ;
33
-
34
- // Display the result
35
- document . querySelector ( '#output' ) . innerHTML = userDataJSON ;
36
- console . log ( userDataJSON ) ;
34
+
35
+ // Display the results
36
+ const outputString =
37
+ `Customer’s account number: ${ customer . account } <br>
38
+ Customer’s account name: ${ customer . name } <br>
39
+ Customer’s 2nd order number: ${ customer . recentOrders [ 1 ] } <br>
40
+ Customer’s account email: ${ customer . contact . email } ` ;
41
+
42
+ document . querySelector ( '#output' ) . innerHTML = outputString ;
37
43
</ script >
38
44
</ body >
39
45
</ html >
Original file line number Diff line number Diff line change 2
2
< html lang ="en ">
3
3
< head >
4
4
< meta charset ="utf-8 ">
5
- < title > Converting a JSON string to a JavaScript object </ title >
5
+ < title > Stringifying a JavaScript object to JSON </ title >
6
6
< style >
7
7
body {
8
8
margin : 1em ;
9
9
font-size : 1.5em ;
10
10
}
11
+ pre {
12
+ margin-top : .5em ;
13
+ }
11
14
</ style >
12
15
</ head >
13
16
< body >
17
+ < pre id ="output ">
18
+ </ pre >
19
+
14
20
< script >
15
- // Declare a JSON string
16
- const userDataJSON = `{
17
- "bgColor": "darkolivegreen",
18
- "textColor": "antiquewhite",
19
- "textSize": 20,
20
- "typefaces": [
21
- "Georgia",
22
- "Verdana",
23
- "serif"
24
- ],
25
- "subscriber": true,
26
- "subscriptionType": 3
27
- }` ;
21
+ // Declare a JavaScript object
22
+ const userData = {
23
+ bgColor : "darkolivegreen" ,
24
+ textColor : "antiquewhite" ,
25
+ textSize : "1.25em" ,
26
+ typefaces : [ "Georgia" , "Verdana" , "serif" ] ,
27
+ subscriber : true ,
28
+ subscriptionType : 3
29
+ }
28
30
29
- // Parse it
30
- const userData = JSON . parse ( userDataJSON ) ;
31
+ // Stringify it
32
+ const userDataJSON = JSON . stringify ( userData , null , " " ) ;
31
33
32
34
// Display the result
33
- console . log ( userData ) ;
35
+ document . querySelector ( '#output' ) . innerHTML = userDataJSON ;
36
+ console . log ( userDataJSON ) ;
34
37
</ script >
35
38
</ body >
36
39
</ html >
Original file line number Diff line number Diff line change 2
2
< html lang ="en ">
3
3
< head >
4
4
< meta charset ="utf-8 ">
5
- < title > Adding Data to Web Storage </ title >
5
+ < title > Converting a JSON string to a JavaScript object </ title >
6
6
< style >
7
7
body {
8
8
margin : 1em ;
12
12
</ head >
13
13
< body >
14
14
< script >
15
- // Declare a JavaScript object
16
- const userData = {
17
- bgColor : "darkolivegreen" ,
18
- textColor : "antiquewhite" ,
19
- textSize : "1.25em" ,
20
- typefaces : [ "Georgia" , "Verdana" , "serif" ] ,
21
- subscriber : true ,
22
- subscriptionType : 3
23
- }
15
+ // Declare a JSON string
16
+ const userDataJSON = `{
17
+ "bgColor": "darkolivegreen",
18
+ "textColor": "antiquewhite",
19
+ "textSize": "1.25em",
20
+ "typefaces": [
21
+ "Georgia",
22
+ "Verdana",
23
+ "serif"
24
+ ],
25
+ "subscriber": true,
26
+ "subscriptionType": 3
27
+ }` ;
28
+
29
+ // Parse it
30
+ const userData = JSON . parse ( userDataJSON ) ;
24
31
25
- // Store it
26
- localStorage . setItem ( 'user-data' , JSON . stringify ( userData ) ) ;
32
+ // Display the result
33
+ console . log ( userData ) ;
27
34
</ script >
28
35
</ body >
29
36
</ html >
Original file line number Diff line number Diff line change 2
2
< html lang ="en ">
3
3
< head >
4
4
< meta charset ="utf-8 ">
5
- < title > Getting Data from Web Storage</ title >
5
+ < title > Adding Data to Web Storage</ title >
6
6
< style >
7
7
body {
8
8
margin : 1em ;
12
12
</ head >
13
13
< body >
14
14
< script >
15
- // Get the data from storage
16
- const userData = JSON . parse ( localStorage . getItem ( 'user-data' ) ) ;
15
+ // Declare a JavaScript object
16
+ const userData = {
17
+ bgColor : "darkolivegreen" ,
18
+ textColor : "antiquewhite" ,
19
+ textSize : "1.25em" ,
20
+ typefaces : [ "Georgia" , "Verdana" , "serif" ] ,
21
+ subscriber : true ,
22
+ subscriptionType : 3
23
+ }
17
24
18
- // Display it
19
- console . log ( userData ) ;
25
+ // Store it
26
+ localStorage . setItem ( 'user-data' , JSON . stringify ( userData ) ) ;
20
27
</ script >
21
28
</ body >
22
29
</ html >
Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > Getting Data from Web Storage</ title >
6
+ < style >
7
+ body {
8
+ margin : 1em ;
9
+ font-size : 1.5em ;
10
+ }
11
+ </ style >
12
+ </ head >
13
+ < body >
14
+ < script >
15
+ // Get the data from storage
16
+ const userData = JSON . parse ( localStorage . getItem ( 'user-data' ) ) ;
17
+
18
+ // Display it
19
+ console . log ( userData ) ;
20
+ </ script >
21
+ </ body >
22
+ </ html >
You can’t perform that action at this time.
0 commit comments