What is JSON?
- JSON stands for JavaScript Object Notation
- JSON is lightweight text-data interchange format
- JSON is language independent *
- JSON is “self-describing” and easy to understand
- Can use JSON like a webservice
JSON is built on two structures:
- A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
Creating JSON:
We can create JSON data with use of json_encode() in php
$json_data = array ('id'=>1,'name'=>"rolf",'country'=>'russia',"office"=>array("google","oracle")); echo json_encode($json_data);
Parsing JSON:
We can parse JSON data using json_decode() in php.
$json_string='{"id":1,"name":"rolf","country":"russia","office":["google","oracle"]} '; $obj=json_decode($json_string); //print the parsed data echo $obj->name; //rolf echo $obj->office[0]; //google