Using Jquery we will develop code to handle Json data. For this we will use getJSON function and then using each looping we will display data.
We will start with simple Json object with a set of name value pairs. We have three examples here and each button is connected to one click event.
To display the return data we have div element.
The Json data or the Json string is kept inside a .php file and for this example we have kept the string inside json1.php file.
Here is the string in Json format. You can keep in any file you want but output should match Json format.
In the above example 'Show Data' button is connected to click event inside Jquery function block.
WE read the Json string stored inside json1.php file.
$.getJSON("json1.php", function(return_data){
In above code we are reading the json string and storing them in return_data variable.
Then we are using one each looping to display all name value pairs.
Here is the complete code.
The use the above demo you can download the zip file. Inside the main file is getJSON-demo.php file. The Json data string are kept inside json1.php, json2.php and json3.php file. Google SDN is used to link Json library so ensure that you are connected to internet while testing the script or you can download and use the Json library.