Json
Json
{ "name" : "value" }
{ "name": "value" }
{ "name1": "value",
"name2": "value",
"name3": "value" }
Resources:
http://myjson.com - Storage of JSON
Create an object
Try in Lint
Structured Data
var myJSON = {}
myJSON.car1 = "black"
myJSON["car1"] = "blue"
console.log(myJSON)
Try It
<div id="output1"></div><div id="output2"></div>
<script>
var output1 =document.getElementById('output1');
var output2 =document.getElementById('output2');
Create an object from scratch
var myObj = { "firstName" : "Mike" ,"lastName" : "Smith" ,
Output the content in the console. "age": 30 };
console.log(myObj);
Add it to your website using JavaScript.
var name = 'Name';
output1.innerHTML = myObj.firstName;
output2.innerHTML = myObj['last' + name];
</script>
Array of items
Better way
for(var i=0;i<myObj.people.length;i++){
output1.innerHTML += "<br>"+myObj.people[i].firstName
+ " " + myObj.people[i].lastName ;
}
</script>
Try It var myObj = {
"people": [
{
"firstName": "Mike",
"lastName": "Smith",
"age": 30
},
Loop Through the items in the object {
"firstName": "John",
array. Output a list of places into your "lastName": "Jones",
HTML. "age": 40
}],
"places":[
{
"location":"Toronto",
"lat":87,
"long":140
},
{
"location":"New York",
"lat":67,
"long":110
}]
};
Try This <div id="output1"></div>
<div id="output2"></div>
<script>
var output1 = document.getElementById('output1');
var output2 = document.getElementById('output2');
Add a new people value to the object using var myObj = {
"people": [
JavaScript. Add to the Object. {
"firstName": "Mike",
"lastName": "Smith",
Loop through the data and output it in the page. "age": 30
},
{
"firstName": "John",
"lastName": "Jones",
"age": 40
}]
};
console.log(myObj);
var name = 'Name';
var i = 0;
output1.innerHTML = myObj.people[i].firstName;
output2.innerHTML = myObj.people[i]['last' + name];
var i = 1;
output1.innerHTML += myObj.people[i].firstName;
output2.innerHTML += myObj.people[i]['last' + name];
</script>
Solution var myObj = {
"people": [
{
"firstName": "Mike",
Add a new people value to the object using "lastName": "Smith",
"age": 30
JavaScript. },
{
"firstName": "John",
Loop through the data and output it in the page. "lastName": "Jones",
"age": 40
}]
};
var temp = {
"firstName": "Linda",
"lastName": "Java",
"age": 22
};
myObj.people.push(temp);
JavaScript Methods
The JSON.stringify() method converts a JavaScript var json = '{"result":true, "count":42}';
value to a JSON string, optionally replacing values var obj = JSON.parse(json);
if a replacer function is specified or optionally
console.log(JSON.stringify({ x: 5, y: 6 }));
including only the specified properties if a // expected output: "{"x":5,"y":6}"
replacer array is specified.
Create an object
https://www.udemy.com/user/lars51/
Laurence Svekis