JSON Lecture PDF
JSON Lecture PDF
Introduction to JSON
http://php.net/manual/en/function.json-decode.php
https://www.javatpoint.com/ajax-tutorial
https://www.w3resource.com/JSON/introduction.php
What is JSON
File: myfile.json
{"employees":[
{"name":"Gianne Mae", "email":"[email protected]"},
{"name":"Juan Paolo", "email":"[email protected]"},
{"name":"Sita", "email":"[email protected]"}
]}
Introduction to JSON
Features of JSON
1. Simplicity
2. Openness
3. Self Describing
4. Internationalization
5. extensibility
6. Interoperability
Introduction to JSON
No. JSON XML
JSON vs XML
JSON stands for JavaScript Object
1 XML stands for eXtensible Markup Language
Notation
2 JSON is simple to read and write XML is less simple than JSON
JSON doesn't provide display XML provides the capability to display data
5
capabilities because it is a markup language
XML Example
<employees>
<employee>
<name>Gianne Mae</name>
<email>[email protected]</email>
</employee>
<employee>
<name>Juan Paolo</name>
<email>[email protected]</email>
</employee>
<employee>
<name>Sita</name>
<email>[email protected]</email>
</employee>
</employees>
Introduction to JSON
Structure of JSON
Introduction to JSON
Similarities between JSON and XML
JSON supports numbers in double precision floating-point format. The number can be
digits (0-9), fractions(.33, .532 etc) and exponent (e, e+, e-, E, E+, E-).
{
"integer": 34,
"fraction": .2145,
"exponent": 6.61789e+0
}
Introduction to JSON
JSON Object with Booleans
We can store array inside JSON array, it is known as array of arrays or multi-dimentional
array.
[
[ "a", "b", "c" ],
[ "m", "n", "o" ],
[ "x", "y", "z" ]
]
Introduction to JSON
JSON Comments
For example.
{
"employee":{
"name":"Mhen",
"salary":56000,
"comments":"He is a nice man"
}
}
The json_encode() function returns the JSON representation of a value. It converts PHP
variable (containing array) into JSON.
Syntax:
string json_encode ( mixed $value [, int $options = 0
[, int $depth = 512 ]] )
Introduction to JSON
PHP JSON
Output:
{"a":1,"b":2,"c":3,"d":4,"e":5}
Introduction to JSON
PHP JSON
Output:
{"firstName":"Joey","lastName":"De Leon","email":"[email protected]"}
Introduction to JSON
PHP JSON
PHP json_decode
The json_decode() function decodes the JSON string. In other words, It converts JSON
string into a PHP variable.
Syntax:
<!DOCTYPE html>
<html>
<body>
<h2>Convert a string into a date object.</h2>
<p id="demo"></p>
<script>
var text = '{"name":"Joselito", "birth":"1965-08-06",
"address":"Sta. Mesa, Manila"}';
var obj = JSON.parse(text);
obj.birth = new Date(obj.birth);
document.getElementById("demo").innerHTML = obj.name + ", "
+obj.birth;
</script>
</body>
</html>
Introduction to JSON
Parsing Dates
Other solution for date using the second parameter of the JSON.parse() function, called reviver.
The reviver parameter is a function that checks each property, before returning the value.
<!DOCTYPE html>
<html>
<body>
<h2>Convert a string into a date object.</h2>
<p id="demo"></p>
<script>
var text = '{"name":"Joselito", "birth":"1965-08-06", "city":"Sta. Mesa,
Manila"}';
var obj = JSON.parse(text, function (key, value) {
if (key == "birth") {
return new Date(value);
} else {
return value;
}
});
document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;
</script>
</body>
</html>
Introduction to JSON
JSON.stringify()
document.getElementById("demo").innerHTML = JSONstr;
<!DOCTYPE html>
<html>
<body>
<h2>Create JSON string from a JavaScript object.</h2>
<p id="demo"></p>
<script>
var obj = { name: "Joselito", age: 25, city: "Sta. Mesa, Manila" };
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
Introduction to JSON
JSON.stringify()
<!DOCTYPE html>
<html>
<body>
<h2>Create JSON string from a JavaScript array.</h2>
<p id="demo"></p>
<script>
var arr = [ "Enelra", "Rose", "Gianne", "Paolo" ];
var JSONstr = JSON.stringify(arr);
document.getElementById("demo").innerHTML = JSONstr;
</script>
</body>
</html>
Introduction to JSON
JSON.HTML