View difference between Paste ID: zeQrh699 and 1yjxVh28
SHOW: | | - or go back to the newest paste.
1-
main:
1+
testing.html:
2
<!DOCTYPE html>
3
<html>
4
<body>
5
<h2>Create Object from JSON String</h2>
6
<p>
7
First Name: <span id="fname"></span><br> 
8
Last Name: <span id="lname"></span><br> 
9
</p> 
10
<script src = smth.json></script>
11
<script>
12
13
var obj = JSON.parse(txt)
14
15
document.getElementById("fname").innerHTML=obj.employees[1].firstName 
16
document.getElementById("lname").innerHTML=obj.employees[1].lastName 
17
</script>
18
</body>
19
</html>
20
21
smth.json:
22
23-
txt = '{"employees":[ "{firstName":"John","lastName":"Doe" }', '{"firstName":"Anna","lastName":"Smith" }', '{"firstName":"Peter","lastName":"Jones" }
23+
txt = {
24-
]}';
24+
    "employees": [ 
25
	{"firstName":"John",
26
	"lastName":"Doe" }, 
27
	{"firstName":"Anna",
28
	"lastName":"Smith" }, 
29
	{"firstName":"Peter",
30
	"lastName":"Jones" }
31
]};