-
Notifications
You must be signed in to change notification settings - Fork 0
/
07 json objects literals.html
65 lines (57 loc) · 2.86 KB
/
07 json objects literals.html
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
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>json objects literals</title>
</head>
<body>
<pre>
This is a JSON string: '{"name":"John", "age":30, "car":null}'
Inside the JSON string there is a JSON object literal: {"name":"John", "age":30, "car":null}
JSON object literals are surrounded by curly braces {}.
JSON object literals contains key/value pairs.
Keys and values are separated by a colon.
Keys must be strings, and values must be a valid JSON data type:
string
number
object
array
boolean
null
Each key/value pair is separated by a comma.
NOTE:
It is a common mistake to call a JSON object literal "a JSON object".
JSON cannot be an object. JSON is a string format.
The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.
</pre>
<script>
//JavaScript Objects
//You can create a JavaScript object from a JSON object literal:
const obj={"name":"Muazim","age":23, "inSchool":null};
console.log(obj.name); //this is called object accessing using dot(.)
//i.e accessing objects value using dot
//Normally, you create a JavaScript object by parsing a JSON string:
//Example
const myJSON = '{"name":"John", "age":30, "car":null}';
const myObj = JSON.parse(myJSON);
console.log(myObj.age)
//we can also access object value using ["keyname"]
console.log(myObj["name"]);
console.log(myObj["car"]);
//Looping an Object
//You can loop through object properties with a for-in loop:
const json2='{"item":"car","model":"2022","type":"4x4","topSpeed":"220-KMPH","brand":"BMW"}';
const obj2=JSON.parse(json2);
console.log(obj2)
let text="";
for(const i in obj2){
//text += i +", "; // this will only access key's not their value i.e : item,model,type,...etc
//In a for-in loop, use the bracket notation to access the property values i.e: car, 2022, 4x4,... etc
text += obj2[i] + ", "; //this will only access value of key's
}
console.log(text);
</script>
</body>
</html>