×
Clear all filters including search bar
Valeri Tandilashvili's JSON Notes
Object
- a collection of key/value pairs
Array
- an ordered list of valuesvar obj = { name: "John", age: 30, city: "New York" };
// Converts object into JSON string
var myJSON = JSON.stringify(obj);
// Puts the string into element with id: demo
document.getElementById("demo").innerHTML = myJSON;
JSON.parse()
Parses the data and converts it into a JavaScript objectlet txt = '{"name":"John", "age":30, "city":"New York"}'
// Converts JSON string into a JavaScript object.
let obj = JSON.parse(txt);
// Puts the data into "p" element with id: demo
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
- a function
- a date
- a symbol
- undefined
,
These are enclosed in square brackets []
which means that array begins with [
and ends with ]
{
"books": [
{ "language":"Java" , "edition":"second" },
{ "language":"C++" , "lastName":"fifth" },
{ "language":"C" , "lastName":"third" }
]
}
JSON
stands for JavaScript Object Notation
JSON
is a way of communicating data with specific rules
JSON
is a syntax for storing and exchanging data
JSON
is text, written with JavaScript object notation
The syntax is taken from JavaScript
but JSON
is portable with other languages
JSON
is a lightweight data-interchange format
It's easy for humans to read and write
It's easy for machines to parse and generate
A common use of JSON is to exchange data to/from
a web server
application/json
is the official Internet media type for JSON
.json
is the JSON
filename extensionDynamically
changes HTML table content using XMLHttpRequest after choosing any option on <select>
obj = { table: "customers", limit: 20 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
txt += "<table border='1'>"
for (x in myObj) {
txt += "<tr><td>" + myObj[x].name + "</td></tr>";
}
txt += "</table>"
document.getElementById("demo").innerHTML = txt;
}
}
xmlhttp.open("POST", "json_demo_db_post.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);