JSON Tutorials

json160JSON (JavaScript Object Notation), is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is used by many other technologies like PHP and Java for data interchange format over the Internet.

What is JSON?
JSON is ultra-weight data interchange data format used over the internet for transferring the data. While XML is a dominant data interchange format over the internet but JSON is less complex and light-weight data.
Though it was first made to be used with JavaScript for accessing remote data, it is now used by many other languages because JSON data is platform independent data format.

Data Types and Example of JSON data
JSON supports various kind of data types which included numbers, strings, booleans as well as array datas and obviously object (collection of key:value pairs, comma-separated and enclosed in curly brackets).

Sample format of JSON data for a detail of a employee,

Creating and Parsing JSON data format in PHP
To handle JSON data there is JSON extension in PHP which is aviable after PHP 5.2.0.

Two funcitons : json_encode() and json_decode() are very useful converting and parsing JSON data through PHP.

Note:These function only works with UTF-8 encoded data.

Description: json_encode()

string json_encode ( mixed $value [, int $options = 0 ] ) > Returns a string containing the JSON representation of value.

Description: json_decode()

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ) > Takes a JSON encoded string and converts it into a PHP variable.

json: The json string being decoded.
assoc: When TRUE, returned objects will be converted into associative arrays.
depth: User specified recursion depth.

First of all, let’s look at the PHP code to create the JSON data format of above example using array of PHP.
$json_data = array (‘id’=>1,’name’=>”mike”,’country’=>’usa’,”office”=>array(“microsoft”,”oracle”));
echo json_encode($json_data);

The above code generates the JSON data exactly as above. Now, let’s decode above JSON data in PHP.

$json_string='{"id":1,"name":"mike","country":"usa","office":["microsoft","oracle"]} ';
Now, the $obj variable contains JSON data parsed in PHP object which you can display using code below.
echo $obj->name; //displays mike
echo $obj->office[0]; //displays microsoft
As you can guess,$obj->office is an array and you can loop through it using foreach loop of PHP,
foreach($obj->office as $val)
echo $val

If you like the post & want to get more then Subscribe our blog posts. Subscribe SunwareTech.net RSS Feed Facebook Twitter Email

Both comments and pings are currently closed.

6 Responses to “JSON Tutorials”

  1. vikashgupta says:

    good very useful

  2. good share 🙂 🙂 🙂

  3. WONDERFUL Post.thanks for share..extra wait .. …

  4. I really liked your article. Fantastic.

  5. vimax says:

    I have a comment right here although not about this post, it really is regarding the website layout. I believe you should alter every now and then to generate it intersting to folks.

  6. facebook123 says:

    Superb website…

    […]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[…]……

Copyright © Sunware Technologies - Your web partner