JSON
JSON
* Javascript object notation.
* JSON is a syntax for storing and exchanging data.
* JSON is text,written with javascript object notation
1.what is JSON?
- Json stands for javascript object notation.
- JSON is a lightweight data-interchange format.
- JSON is ¨self-describing¨ and easy to understad.
- JSON is language independent.
1.Why use JSON?
- since the jSON format is text only.it use easily be sent to and form a server.and used as a data format by any programming language.
- Javascript has a built in function to convert a string. written in JSON format. into native javascript.
JSON.parse()
- So if you receive data from a server in JSON format. you can use it like any other javascript object.
Exchanging Data
- when exchanging data between a browser and a server,the data can only be text.
- JSON is text,andwe can convert any javascript object into JSON, and send JSON to the server .
- we can also convert any JSON received from the server into javascript objects.
- If you have data stored in a javascript object.you can convert the object into JSON, and send it to a server.
Receiving Data
- If you have receive data in JSON format.you can convert it into a javascript object.
Examble:
Storing Data
- When storing data, the data has to be a certain format , and regardless of where you choose to store it text is always one of the legal formats.
Examble:
JSON Syntax
* The JSON syntax is a subset of the javascript syntax.* JSON Syntax Rules1.Data is in name/value pairs.2.Data is separated by commas.3.Curly braces hold objects.4.Square brackets hold arays* JSON ---{"name":"mayoori"}* Javascript ---{name:"mayoori"}JSON datatypes1.String
{"name":"mayoori"}2.Number
{"age":20}3.Booleans
{"sale":true}4.Objects
{ "employee"{"name":"mayoori","age":20,"city":"jaffna"}
}
5.Arrays
{"employee":
["thanu","siva","jano"]
}6.Null
{"middlename":null}we can access a javscript obect like this
* var person ={"name":"mayoorika","age":"20"};
2.person["name"];
3.person.name="thanusha";
4.person.["name"]="thanu";
Objects
- JSON objects are surrounded by curly breces{}.
- JSON object are written in key/value pairs.
- Key must be strings,and values must be a valid json data type(string,number,boolean,object,null).
assessing object valus
1. (.) Notation.
myobj={"name":"mayoo","age":20};
x=myobj.name;
2.([]) bracket notation.
myobj={"name":"mayoo","age":20};
x=myobj["name"] ;
3.Looping an object.
myobj={"name":"mayoo","age":20};
for(x in myobj)
{document.getElementById("demo").innerHTML +=x;}
Comments