By 05/02/2014 4 Comments

How to Parse and Create JSON data in PHP

What is JSON data?

  • It stands for Javascript Object Notation
  • It is a lightweight and cross platform data-interchange format
  • It is easy for humans, they can read and write
  • It can be easily generated and parsed by the computers
  • It is based on javascript programming language
  • It also can be used for exchanging and storing data from server to client and vice versa like XML

Can JSON be parsed using PHP?

  • JSON can be parsed and created easily using PHP which has define as following

Following is the code for parse and create JSON in PHP.

<?php

$json_data = array ('id'=>1,'name'=>"xyz",'country'=>'abc',"office"=>array("cde","egf")); // create an array with key and values

echo json_encode($json_data); // this function parsed or convert above array into JSON format and create JSON array OBJECT, pass 1 parameter which is an array and also display JSON OBJECT string in webpage

$json_string='{"id":1,"name":"xyz","country":"abc","office":["cde","egf"]} '; // this line creates a string which is a JSON object string and assign that JSON string into $json_string variable

$obj=json_decode($json_string); // this function pass 1 parameter which is above $json_string.this function takes a JSON string and convert it into PHP variable, assign into $obj

//print the parsed data

echo $obj->name; //displays xyz

echo $obj->office[0]; //displays cde

?>






Posted in: PHP

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .