(Go: >> BACK << -|- >> HOME <<)

SlideShare a Scribd company logo
JSON
(JavaScript Object Notation)
infobizzs.com
What is JSON?
• JSON stands for JavaScript Object Notation
• JSON is a lightweight data-interchange format
• JSON is language independent *
• JSON is "self-describing" and easy to understand
• JSON is a syntax for storing and exchanging data.
• JSON is an easier-to-use alternative to XML.
infobizzs.com
JSON Example<!DOCTYPE html>
<html>
<body>
<h2>JSON Object Creation in JavaScript</h2>
<p id="demo"></p>
<script>
var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.name + "<br>" +
obj.street + "<br>" +
obj.phone;
</script>
</body>
</html> infobizzs.com
Output
infobizzs.com
 Much Like XML Because
• Both JSON and XML is "self describing" (human readable)
• Both JSON and XML is hierarchichal (values within values)
• Both JSON and XML can be parsed and used by lots of programming languages
• Both JSON and XML can be fetched with an XMLHttpRequest
 Much Unlike XML Because
• JSON doesn't use end tag
• JSON is shorter
• JSON is quicker to read and write
• JSON can use arrays
• The biggest difference is:
XML has to be parsed with an XML parser, JSON can be parsed by a standard JavaScript function.
infobizzs.com
Why JSON?
For AJAX applications, JSON is faster and easier than XML:
• Using XML
• Fetch an XML document
• Use the XML DOM to loop through the document
• Extract values and store in variables
• Using JSON
• Fetch a JSON string
• JSON.Parse the JSON string
infobizzs.com
JSON Syntax
The JSON syntax is a subset of the JavaScript syntax.
• JSON Syntax Rules
JSON syntax is derived from JavaScript object notation syntax:
• Data is in name/value pairs
• Data is separated by commas
• Curly braces hold objects
• Square brackets hold arrays
infobizzs.com
JSON Data - A Name and aValue
• JSON data is written as name/value pairs.
• A name/value pair consists of a field name (in double quotes), followed by a colon, followed
by a value:
"firstName":"John"
• JSONValues
• JSON values can be:
• A number (integer or floating point)
• A string (in double quotes)
• A Boolean (true or false)
• An array (in square brackets)
• An object (in curly braces)
• null
infobizzs.com
JSON Objects
• JSON objects are written inside curly braces.
• Just like JavaScript, JSON objects can contain multiple name/values pairs:
{"firstName":"John", "lastName":"Doe"}
JSON Arrays
• JSON arrays are written inside square brackets.
• Just like JavaScript, a JSON array can contain multiple objects:
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
infobizzs.com
JSON Uses JavaScript Syntax
• Because JSON syntax is derived from JavaScript object notation, very little
extra software is needed to work with JSON within JavaScript.
• With JavaScript you can create an array of objects and assign data to it, like
this:
var employees = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName": "Jones"}
];
infobizzs.com
JSON HowTo
• A common use of JSON is to read data from a web server, and display the data in a web
page.
• For simplicity, this can be demonstrated by using a string as input (instead of a file).
• JSON Example - Object From String
• Create a JavaScript string containing JSON syntax:
var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
infobizzs.com
• JSON syntax is a subset of JavaScript syntax.
• The JavaScript function JSON.parse(text) can be used to convert a JSON text into a
JavaScript object:
var obj = JSON.parse(text);
Use the new JavaScript object in your page:
• Example
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>
infobizzs.com
JSON Http Request
• A common use of JSON is to read data from a web server, and display the data in a web page.
• This chapter will teach you, in 4 easy steps, how to read JSON data, using XMLHttp.
• This example reads a menu from myTutorials.txt, and displays the menu in a web page:
<div id="id01"></div>
<script>
var xmlhttp = new XMLHttpRequest();
var url = "myTutorials.txt";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
}
infobizzs.com
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var out = "";
var i;
for(i = 0; i < arr.length; i++) {
out += '<a href="' + arr[i].url + '">' +
arr[i].display + '</a><br>';
}
document.getElementById("id01").innerHTML = out;
}
</script>
infobizzs.com
Output
infobizzs.com
Example Explained
1: Create an array of objects.
• Use an array literal to declare an array of objects.
• Give each object two properties: display and url.
• Name the array myArray:
myArray
var myArray = [
{
"display": "JavaScriptTutorial",
"url": "http://www.w3schools.com/js/default.asp"
},
{
"display": "HTMLTutorial",
"url": "http://www.w3schools.com/html/default.asp"
},
{
"display": "CSSTutorial",
"url": "http://www.w3schools.com/css/default.asp"
}
]
infobizzs.com
2: Create a JavaScript function to display the array.
• Create a function myFunction() that loops the array objects, and display the
content as HTML links:
myFunction()
function myFunction(arr) {
var out = "";
var i;
for(i = 0; i < arr.length; i++) {
out += '<a href="' + arr[i].url + '">' + arr[i].display + '</a><br>';
}
document.getElementById("id01").innerHTML = out;
}
infobizzs.com
3: Create a text file
• Put the array literal in a file named myTutorials.txt:
myTutorials.txt
[
{
"display": "JavaScriptTutorial",
"url": "http://www.w3schools.com/js/default.asp"
},
{
"display": "HTMLTutorial",
"url": "http://www.w3schools.com/html/default.asp"
},
{
"display": "CSSTutorial",
"url": "http://www.w3schools.com/css/default.asp"
}
]
infobizzs.com
4: Read the text file with an XMLHttpRequest
• Write an XMLHttpRequest to read the text file, and use myFunction() to display
the array:
var xmlhttp = new XMLHttpRequest();
var url = "myTutorials.txt";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
infobizzs.com
Output
infobizzs.com

More Related Content

What's hot

Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
Bala Narayanan
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
Manish Shekhawat
 
Json
JsonJson
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
Mohammed Arif
 
Javascript
JavascriptJavascript
Javascript
mussawir20
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
JavaScript Object Notation (JSON)
JavaScript Object Notation (JSON)JavaScript Object Notation (JSON)
JavaScript Object Notation (JSON)
BOSS Webtech
 
Json
JsonJson
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Xpath presentation
Xpath presentationXpath presentation
Javascript basics
Javascript basicsJavascript basics
Javascript basics
shreesenthil
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 
Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
Eliran Eliassy
 
Java script ppt
Java script pptJava script ppt
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
Shawn Calvert
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
Javascript
JavascriptJavascript
jQuery
jQueryjQuery

What's hot (20)

Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Json
JsonJson
Json
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
Javascript
JavascriptJavascript
Javascript
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
JavaScript Object Notation (JSON)
JavaScript Object Notation (JSON)JavaScript Object Notation (JSON)
JavaScript Object Notation (JSON)
 
Json
JsonJson
Json
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Javascript
JavascriptJavascript
Javascript
 
jQuery
jQueryjQuery
jQuery
 

Similar to Json

JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)
Skillwise Group
 
Json tutorial, a beguiner guide
Json tutorial, a beguiner guideJson tutorial, a beguiner guide
Json tutorial, a beguiner guide
Rafael Montesinos Muñoz
 
Basics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examplesBasics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examples
Sanjeev Kumar Jaiswal
 
JSON
JSONJSON
JSON
Yoga Raja
 
JSON & AJAX.pptx
JSON & AJAX.pptxJSON & AJAX.pptx
JSON & AJAX.pptx
dyumna2
 
A Higher-Order Data Flow Model for Heterogeneous Big Data
A Higher-Order Data Flow Model for Heterogeneous Big DataA Higher-Order Data Flow Model for Heterogeneous Big Data
A Higher-Order Data Flow Model for Heterogeneous Big Data
Simon Price
 
AJAX
AJAXAJAX
Introduction to JSON
Introduction to JSONIntroduction to JSON
Introduction to JSON
Kanda Runapongsa Saikaew
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
Raghu nath
 
Hands on JSON
Hands on JSONHands on JSON
Hands on JSON
Octavian Nadolu
 
J s-o-n-120219575328402-3
J s-o-n-120219575328402-3J s-o-n-120219575328402-3
J s-o-n-120219575328402-3
Ramamohan Chokkam
 
Json
JsonJson
Json
soumya
 
Json
JsonJson
Json the-x-in-ajax1588
Json the-x-in-ajax1588Json the-x-in-ajax1588
Json the-x-in-ajax1588
Ramamohan Chokkam
 
Json at work overview and ecosystem-v2.0
Json at work   overview and ecosystem-v2.0Json at work   overview and ecosystem-v2.0
Json at work overview and ecosystem-v2.0
Boulder Java User's Group
 
java script json
java script jsonjava script json
java script json
chauhankapil
 
JSON Fuzzing: New approach to old problems
JSON Fuzzing: New  approach to old problemsJSON Fuzzing: New  approach to old problems
JSON Fuzzing: New approach to old problems
titanlambda
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
guestfd7d7c
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college project
AmitSharma397241
 
Json
JsonJson

Similar to Json (20)

JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)
 
Json tutorial, a beguiner guide
Json tutorial, a beguiner guideJson tutorial, a beguiner guide
Json tutorial, a beguiner guide
 
Basics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examplesBasics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examples
 
JSON
JSONJSON
JSON
 
JSON & AJAX.pptx
JSON & AJAX.pptxJSON & AJAX.pptx
JSON & AJAX.pptx
 
A Higher-Order Data Flow Model for Heterogeneous Big Data
A Higher-Order Data Flow Model for Heterogeneous Big DataA Higher-Order Data Flow Model for Heterogeneous Big Data
A Higher-Order Data Flow Model for Heterogeneous Big Data
 
AJAX
AJAXAJAX
AJAX
 
Introduction to JSON
Introduction to JSONIntroduction to JSON
Introduction to JSON
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Hands on JSON
Hands on JSONHands on JSON
Hands on JSON
 
J s-o-n-120219575328402-3
J s-o-n-120219575328402-3J s-o-n-120219575328402-3
J s-o-n-120219575328402-3
 
Json
JsonJson
Json
 
Json
JsonJson
Json
 
Json the-x-in-ajax1588
Json the-x-in-ajax1588Json the-x-in-ajax1588
Json the-x-in-ajax1588
 
Json at work overview and ecosystem-v2.0
Json at work   overview and ecosystem-v2.0Json at work   overview and ecosystem-v2.0
Json at work overview and ecosystem-v2.0
 
java script json
java script jsonjava script json
java script json
 
JSON Fuzzing: New approach to old problems
JSON Fuzzing: New  approach to old problemsJSON Fuzzing: New  approach to old problems
JSON Fuzzing: New approach to old problems
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college project
 
Json
JsonJson
Json
 

Recently uploaded

Capitol Doctoral Presentation -June 2024v2.pptx
Capitol Doctoral Presentation -June 2024v2.pptxCapitol Doctoral Presentation -June 2024v2.pptx
Capitol Doctoral Presentation -June 2024v2.pptx
CapitolTechU
 
Principles of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptxPrinciples of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptx
ibtesaam huma
 
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
PECB
 
The membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERPThe membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERP
Celine George
 
220711130045_PRIYA_DAS_M.S___Access__ppt
220711130045_PRIYA_DAS_M.S___Access__ppt220711130045_PRIYA_DAS_M.S___Access__ppt
220711130045_PRIYA_DAS_M.S___Access__ppt
Kalna College
 
Webinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional SkillsWebinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional Skills
EduSkills OECD
 
NLC English INTERVENTION LESSON 3-D1.pptx
NLC English INTERVENTION LESSON 3-D1.pptxNLC English INTERVENTION LESSON 3-D1.pptx
NLC English INTERVENTION LESSON 3-D1.pptx
Marita Force
 
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdfARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
DharmarajPawar
 
AI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdfAI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdf
SrimanigandanMadurai
 
Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9
John Rodzvilla
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
Nguyen Thanh Tu Collection
 
Satta Matka Dpboss Kalyan Matka Results Kalyan Chart
Satta Matka Dpboss Kalyan Matka Results Kalyan ChartSatta Matka Dpboss Kalyan Matka Results Kalyan Chart
Satta Matka Dpboss Kalyan Matka Results Kalyan Chart
Mohit Tripathi
 
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISINGSYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
Dr Vijay Vishwakarma
 
Book Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docxBook Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docx
drtech3715
 
NationalLearningCamp-2024-Orientation-for-RO-SDO.pptx
NationalLearningCamp-2024-Orientation-for-RO-SDO.pptxNationalLearningCamp-2024-Orientation-for-RO-SDO.pptx
NationalLearningCamp-2024-Orientation-for-RO-SDO.pptx
CelestineMiranda
 
Delegation Inheritance in Odoo 17 and Its Use Cases
Delegation Inheritance in Odoo 17 and Its Use CasesDelegation Inheritance in Odoo 17 and Its Use Cases
Delegation Inheritance in Odoo 17 and Its Use Cases
Celine George
 
Still I Rise by Maya Angelou | Summary and Analysis
Still I Rise by Maya Angelou | Summary and AnalysisStill I Rise by Maya Angelou | Summary and Analysis
Still I Rise by Maya Angelou | Summary and Analysis
Rajdeep Bavaliya
 
Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17
Celine George
 
debts of gratitude 2 detailed meaning and certificate of appreciation.pptx
debts of gratitude 2 detailed meaning and certificate of appreciation.pptxdebts of gratitude 2 detailed meaning and certificate of appreciation.pptx
debts of gratitude 2 detailed meaning and certificate of appreciation.pptx
AncyTEnglish
 
Final ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdfFinal ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdf
Zuzana Mészárosová
 

Recently uploaded (20)

Capitol Doctoral Presentation -June 2024v2.pptx
Capitol Doctoral Presentation -June 2024v2.pptxCapitol Doctoral Presentation -June 2024v2.pptx
Capitol Doctoral Presentation -June 2024v2.pptx
 
Principles of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptxPrinciples of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptx
 
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
 
The membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERPThe membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERP
 
220711130045_PRIYA_DAS_M.S___Access__ppt
220711130045_PRIYA_DAS_M.S___Access__ppt220711130045_PRIYA_DAS_M.S___Access__ppt
220711130045_PRIYA_DAS_M.S___Access__ppt
 
Webinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional SkillsWebinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional Skills
 
NLC English INTERVENTION LESSON 3-D1.pptx
NLC English INTERVENTION LESSON 3-D1.pptxNLC English INTERVENTION LESSON 3-D1.pptx
NLC English INTERVENTION LESSON 3-D1.pptx
 
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdfARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
 
AI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdfAI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdf
 
Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
 
Satta Matka Dpboss Kalyan Matka Results Kalyan Chart
Satta Matka Dpboss Kalyan Matka Results Kalyan ChartSatta Matka Dpboss Kalyan Matka Results Kalyan Chart
Satta Matka Dpboss Kalyan Matka Results Kalyan Chart
 
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISINGSYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
 
Book Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docxBook Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docx
 
NationalLearningCamp-2024-Orientation-for-RO-SDO.pptx
NationalLearningCamp-2024-Orientation-for-RO-SDO.pptxNationalLearningCamp-2024-Orientation-for-RO-SDO.pptx
NationalLearningCamp-2024-Orientation-for-RO-SDO.pptx
 
Delegation Inheritance in Odoo 17 and Its Use Cases
Delegation Inheritance in Odoo 17 and Its Use CasesDelegation Inheritance in Odoo 17 and Its Use Cases
Delegation Inheritance in Odoo 17 and Its Use Cases
 
Still I Rise by Maya Angelou | Summary and Analysis
Still I Rise by Maya Angelou | Summary and AnalysisStill I Rise by Maya Angelou | Summary and Analysis
Still I Rise by Maya Angelou | Summary and Analysis
 
Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17
 
debts of gratitude 2 detailed meaning and certificate of appreciation.pptx
debts of gratitude 2 detailed meaning and certificate of appreciation.pptxdebts of gratitude 2 detailed meaning and certificate of appreciation.pptx
debts of gratitude 2 detailed meaning and certificate of appreciation.pptx
 
Final ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdfFinal ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdf
 

Json

  • 2. What is JSON? • JSON stands for JavaScript Object Notation • JSON is a lightweight data-interchange format • JSON is language independent * • JSON is "self-describing" and easy to understand • JSON is a syntax for storing and exchanging data. • JSON is an easier-to-use alternative to XML. infobizzs.com
  • 3. JSON Example<!DOCTYPE html> <html> <body> <h2>JSON Object Creation in JavaScript</h2> <p id="demo"></p> <script> var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}'; var obj = JSON.parse(text); document.getElementById("demo").innerHTML = obj.name + "<br>" + obj.street + "<br>" + obj.phone; </script> </body> </html> infobizzs.com
  • 5.  Much Like XML Because • Both JSON and XML is "self describing" (human readable) • Both JSON and XML is hierarchichal (values within values) • Both JSON and XML can be parsed and used by lots of programming languages • Both JSON and XML can be fetched with an XMLHttpRequest  Much Unlike XML Because • JSON doesn't use end tag • JSON is shorter • JSON is quicker to read and write • JSON can use arrays • The biggest difference is: XML has to be parsed with an XML parser, JSON can be parsed by a standard JavaScript function. infobizzs.com
  • 6. Why JSON? For AJAX applications, JSON is faster and easier than XML: • Using XML • Fetch an XML document • Use the XML DOM to loop through the document • Extract values and store in variables • Using JSON • Fetch a JSON string • JSON.Parse the JSON string infobizzs.com
  • 7. JSON Syntax The JSON syntax is a subset of the JavaScript syntax. • JSON Syntax Rules JSON syntax is derived from JavaScript object notation syntax: • Data is in name/value pairs • Data is separated by commas • Curly braces hold objects • Square brackets hold arrays infobizzs.com
  • 8. JSON Data - A Name and aValue • JSON data is written as name/value pairs. • A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value: "firstName":"John" • JSONValues • JSON values can be: • A number (integer or floating point) • A string (in double quotes) • A Boolean (true or false) • An array (in square brackets) • An object (in curly braces) • null infobizzs.com
  • 9. JSON Objects • JSON objects are written inside curly braces. • Just like JavaScript, JSON objects can contain multiple name/values pairs: {"firstName":"John", "lastName":"Doe"} JSON Arrays • JSON arrays are written inside square brackets. • Just like JavaScript, a JSON array can contain multiple objects: "employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName":"Jones"} ] infobizzs.com
  • 10. JSON Uses JavaScript Syntax • Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript. • With JavaScript you can create an array of objects and assign data to it, like this: var employees = [ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName": "Jones"} ]; infobizzs.com
  • 11. JSON HowTo • A common use of JSON is to read data from a web server, and display the data in a web page. • For simplicity, this can be demonstrated by using a string as input (instead of a file). • JSON Example - Object From String • Create a JavaScript string containing JSON syntax: var text = '{ "employees" : [' + '{ "firstName":"John" , "lastName":"Doe" },' + '{ "firstName":"Anna" , "lastName":"Smith" },' + '{ "firstName":"Peter" , "lastName":"Jones" } ]}'; infobizzs.com
  • 12. • JSON syntax is a subset of JavaScript syntax. • The JavaScript function JSON.parse(text) can be used to convert a JSON text into a JavaScript object: var obj = JSON.parse(text); Use the new JavaScript object in your page: • Example <p id="demo"></p> <script> document.getElementById("demo").innerHTML = obj.employees[1].firstName + " " + obj.employees[1].lastName; </script> infobizzs.com
  • 13. JSON Http Request • A common use of JSON is to read data from a web server, and display the data in a web page. • This chapter will teach you, in 4 easy steps, how to read JSON data, using XMLHttp. • This example reads a menu from myTutorials.txt, and displays the menu in a web page: <div id="id01"></div> <script> var xmlhttp = new XMLHttpRequest(); var url = "myTutorials.txt"; xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var myArr = JSON.parse(xmlhttp.responseText); myFunction(myArr); } } infobizzs.com
  • 14. xmlhttp.open("GET", url, true); xmlhttp.send(); function myFunction(arr) { var out = ""; var i; for(i = 0; i < arr.length; i++) { out += '<a href="' + arr[i].url + '">' + arr[i].display + '</a><br>'; } document.getElementById("id01").innerHTML = out; } </script> infobizzs.com
  • 16. Example Explained 1: Create an array of objects. • Use an array literal to declare an array of objects. • Give each object two properties: display and url. • Name the array myArray: myArray var myArray = [ { "display": "JavaScriptTutorial", "url": "http://www.w3schools.com/js/default.asp" }, { "display": "HTMLTutorial", "url": "http://www.w3schools.com/html/default.asp" }, { "display": "CSSTutorial", "url": "http://www.w3schools.com/css/default.asp" } ] infobizzs.com
  • 17. 2: Create a JavaScript function to display the array. • Create a function myFunction() that loops the array objects, and display the content as HTML links: myFunction() function myFunction(arr) { var out = ""; var i; for(i = 0; i < arr.length; i++) { out += '<a href="' + arr[i].url + '">' + arr[i].display + '</a><br>'; } document.getElementById("id01").innerHTML = out; } infobizzs.com
  • 18. 3: Create a text file • Put the array literal in a file named myTutorials.txt: myTutorials.txt [ { "display": "JavaScriptTutorial", "url": "http://www.w3schools.com/js/default.asp" }, { "display": "HTMLTutorial", "url": "http://www.w3schools.com/html/default.asp" }, { "display": "CSSTutorial", "url": "http://www.w3schools.com/css/default.asp" } ] infobizzs.com
  • 19. 4: Read the text file with an XMLHttpRequest • Write an XMLHttpRequest to read the text file, and use myFunction() to display the array: var xmlhttp = new XMLHttpRequest(); var url = "myTutorials.txt"; xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var myArr = JSON.parse(xmlhttp.responseText); myFunction(myArr); } } xmlhttp.open("GET", url, true); xmlhttp.send(); infobizzs.com