How to Read JSON Data in PHP using jQuery Ajax

0 47

How to Read JSON Data using jQuery in PHP, recently i have posted a tutorial about Converting MySQL data into JSON string, as you all know that JSON is easy to understand and light-weight data interchanging format between browsers and servers, so we will see how to read json files and how to read json data, and how to parse json into HTML Table, sometimes we need to get this type of data and it’s useful to create restful API’s, so let’s get stared.


JSON JQuery Syntax

this is the JSON Parsing jQuery syntax which has three parameters, first one is url for calling and getting PHP or JSON file, and the data is object or a string that is sent to the server with the request, and the success is just a callback executed when request succedd, hence we can also use $.ajax()method to get the same thing.

Sample JavaScript

the following code is the complete javascript code which calls php file and and parse the json data into html table. as i explained above $.getJSON() methods takes the url parameter and calls the getjson.php file, and php file contains JSON data.

HTML Table

HTML table, here in this table JSON data are going to be parsed and displayed. we need to give the id of this table in the javascript part id=”json-data”

Sample PHP Code

this is simple PHP code which converts MySQL rows into JSON format, we have already posted this tutorial. using json_encode() we can convert it easily. and i have taken here the main root object as “tutorial”.

Database Connection

Read JSON File

following is the sample data of above created table converted into JSON file as “getposts.json”, now let’s have a look how to read json files. simple just replace the php file with json file, that’s it.
you can validate your JSON Format using : JSON Validator

“;replace php file with json file like this. url=”getposts.json

SON Parsed HTML Output

here’s JSON data are parsed into HTML table data.

()Read Using $.Ajax

as i told we can parse it using $.ajax() also, so following is the example code using $.ajax(), it will give the same output as above.


تعليقات فيس بوك

اضافة تعليق

قد يعجبك ايضا أكثر من مؤلف