JSON
JSON
JSON
What is JSON?
o null
JSON Data Values (Cont.)
1- String:
o Sequence of 0 or more Unicode characters.
o Wrapped in "double quotes“
o Example: "Name": "Ahmed"
JSON Data Values (Cont.)
2- Number:
o Can be: Integer, Real, float, Scientific.
o Can’t be: octal, hex (Use String instead), NaN or Infinity (Use
Null instead).
o Example: "rating": 123
"length": 122.234
"Temp": -5
"atoms per mole": 6.023e+23
JSON Data Values (Cont.)
3- Boolean:
o Example: "active":true
"active":false
4- null:
o Example: "email":null
JSON Data Values (Cont.)
5- Object:
o Objects are unordered containers of key/value pairs
o Objects are wrapped in { }
o (,) separates key/value pairs
o (:) separates keys and values
o Keys are strings
o Values are JSON values
JSON Data Values (Cont.)
5- Object (Cont.):
o Example:
Var jObj=
{
“id”:10,
"company":"ITI" ,
"city": “Assiut"
}
JSON Data Values (Cont.)
5- Object (Cont.):
o JavaScript code Example:
<script>
var sObj= ‘{“ID”:10,"name":“Ali",“City":“Assiut“, “Job”: null,
“Married”: true}’;
var jObj= JSON.parse(sObj);
//OR
var jobj= {ID:10,name:“Ali",City:“Assiut“, Job: null, Married:
true};
//when embded in JS code, keys aren’t qouted.
document.write(jobj.ID);
document.write(jobj.name);
document.write(jobj.Job);
</script>
JSON Data Values (Cont.)
5- Array:
o Arrays are ordered sequences of values
5- Array (Cont.):
oArray of string:
var Arr=["Ahmed", "Ali", "Emad"]
oArray of Numbers:
var Arr =[30, 20, 10]
oArray of Objects:
var employees=
[
{firstName:"John", lastName:"Doe"},
{firstName:"Anna", lastName:"Smith"},
{firstName:"Peter", lastName:"Jones"}
]
JSON Data Values (Cont.)
5- Array (Cont.):
o Example:
<script>
//Array of one type
var stdArr=[“Ahmed”,”Ali”,”Emad”];
for (i=0; i<3; i++)
{
document.write(stdArr[i]);
}
//Array with different types
var Jarr=["red", 1, null, true];
document.write(Jarr[2]);
//Nested array
var Jarr2=["red",1,["a", "b", "c"], false];
document.write(Jarr2[2][1]);
//Object in an array
var Jarr3=[“Ahmed", {Grade:“A", Mark:20}, 2];
document.write(Jarr 3[1].Grade);
</script>
JSON vs. XML
o JSON Is Versionless:
JSON is very stable. No revisions to the JSON grammar are
anticipated.
o JSON Is Not XML:
JSON(Object, Array, String, number, Null).
XML(element, Attribute, attribute string, Content, Entities,
Declarations, Schema, Version, namespace).
Why JSON over XML?
Using JSON:
o Fetch a JSON string
o JSON.Parse the JSON string
JSON Syntax vs. XML Syntax
<wclass>
<!--My students who took web programming class
with me-->
<student id="1">
<name>Linda Jones</name>
<legacySkill>Access, VB5.0</legacySkill>
</student>
<student id="2">
<name>Adam Davidson</name>
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
</wclass>
JSON Syntax vs. XML Syntax (Cont.)
JSON Data representation:
<SCRIPT LANGUAGE="JavaScript">
var webclass =
{wclass:
[
{student:
{
id:1,
name:"Linda Jones",
legacySkill:["Access”, “VB 5.0“]
}
},
{student:
{
id:2,
name:"Adam Davidson",
legacySkill:["Cobol”, “MainFrame“]
}
}
]
};
alert(webclass.wclass[1].student.legacySkill[0]);
// Cobol
</SCRIPT>
Parsing JSON string
You can write JSON objects and arrays directly as objects and arrays
Directly in JavaScript:
<script>
var obj= {ID:10,name:“Ali",City:“Assiut“, Job: null, Married: true};
document.write(obj.ID);
document.write(obj.name);
</script>
You need to parse it to JSON when you read it from text file:
<script>
var jsonStr= ‘{“ID”:10,"name":“Ali",“City":“Assiut“, “Job”: null,
“Married”: true}’;
var obj=JSON.parse(jsonStr);
document.write(obj.ID);
document.write(obj.name);
</script>
Parsing JSON string (Cont.)
For old browsers that don’t support the JavaScript function
JSON.parse() can use the eval() function:
<script>
var jsonStr= ‘{“ID”:10,"name":“Ali",“City":“Assiut“, “Job”: null,
“Married”: true}’;
var obj=eval ("(" + text + ")");
document.write(obj.ID);
document.write(obj.name);
</script>
How to read JSON files from the server?
The file type for JSON files is ".json" (or even .txt files), it’s
just text files.
JSON Http Request: A common use of JSON is to read data
from a web server, and display the data in a web page is using
Http Request (XMLHttpRequest Object).
Http Request is also a technique used in Ajax.
JSON Web resources and References
http://www.json.org
http://www.w3schools.com/json/
<script > </script>
<script>document.writeln(“Thank
You!”)</script>