Title: Json introduction - StudySection
1JSON Introduction
2JSON JavaScript Object Notation
- JSON (JavaScript Object Notation) is a format
that is used to store the data and transport it
to the web pages over the server. - The JSON format is only text but the syntax for
JSON is derived from JavaScript Object Notation.
So code for understanding and writing JSON can be
written in any programming language as JSON is
language independent.
3- Example
- This JSON syntax defines a students object an
array of 2 student records (objects) -
- "students"
- "firstName""shruti", "lastName""arora",
- "firstName""priyal", "lastName""jain"
-
4JSON Syntax Rules
- Data is in name/value pairs
- Pairs must be separated by commas
- Objects must be in curly braces
- Arrays must be in the square brackets container.
JSON Data A Name and a Value Just like
JavaScript object properties, 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 In JSON
name must be in double-quotes. "firstName""shruti
" JavaScript firstNameshruti In JSON string
values must be written in double
quotes. "firstName""shruti" But in javascript,
it can be written in double or single
quotes. firstName"shruti" firstNameshruti
5In JSON, values must be one of the following data
types
- a string
- a number
- an object (JSON object)
- an array
- a boolean
- null
- In JavaScript, values can be all of the above,
plus any other valid JavaScript expression,
including - a function
- a date
- undefined
6- Objects
- JSON objects are written inside curly braces.
- Objects can contain multiple name/value pairs,
Just like in JavaScript - "firstName""shruti", "lastName""arora"
- JSON Arrays
- Inside square brackets, we write JSON arrays .
- Just like in JavaScript, an array can contain
objects - "students"
- "firstName""shruti", "lastName""arora",
- "firstName""priyal", "lastName""jain"
-
- In the example above, the object students is an
array. It contains two objects. - Each object is a record of a student with a first
name and the last name.
7- Converting a JSON Text to a JavaScript Object
- A common use of JSON is to read data from a web
server and display the data on a web page. - For simplicity, this can be demonstrated using a
string as input. - First, create a JavaScript string containing JSON
syntax - var text ' "students" '
- ' "firstName""shruti" , "lastName""arora" ,'
- ' "firstName""priyal" , "lastName""jain"
' - JSON.parse() is a JavaScript built-in function to
convert the string into a javascript object - var obj JSON.parse(text)
8- Sending Data
- If you have a JavaScript object, you can convert
the object into JSON, and send it to a server - Example
- var jsObj name "shruti", age24, city
"Mohali" - var jsonObj JSON.stringify(jsObj)
- window.location "demoJson.php?x" jsonObj
- Receiving Data
- If you receive data in JSON format, you can
convert it into a JavaScript object - Example
- var jsonObj '"name""shruti", "age"24,
"city"" Mohali"' - var myObj JSON.parse(jsonObj)
9- To handle JSON, PHP has some built-in functions
- PHP built-in function json_encode() can be used
to convert an object into JSON - PHP file
- lt?php
- myObj-gtname "Shruti"
- myObj-gtage 24
- myObj-gtcity "MOhali"
- myJSON json_encode(myObj)
- echo myJSON
- ?gt
- Output
- "name""Shruti","age"24,"city""Mohali"
10Online Programing Diploma Exam
- PHP Fullstack Developer Diploma Exam (Foundation)
- C Programming Diploma Certification Exam
(Foundation) - Java Fullstack Developer Diploma Exam
(Foundation) - AWS Developer Diploma Exam (Foundation)
11About Study Section
- Welcome to StudySection - the most loved online
platform for eCertification in several subjects
including but not limited to Software
Development, Quality Assurance, Business
Administration, Project Management, English,
Aptitude and more. From more than 70 countries
students are StudySection Certified. If you are
not yet StudySection certified it's not late. You
can start right now. - Being StudySection Certified helps you take your
education level few notches up and have an edge
over other candidates when you need it the most.
Globally, our students are employed in different
organizations and are utilizing the benefit of
being certified with us.