How PHP returns JSON-formatted data to jquery_php tutorial

Source: Internet
Author: User
JSON-formatted data is the data that we have been using in our application development, such as working with jquery or with the API, using JSON data, so how does PHP return JSON-formatted data to jquery, and let me introduce you to the students.

Manipulating JSON data in jquery we $.parsejson directly (returnstring).

Cases

The code is as follows Copy Code
$ (function () {
$ (' #send '). Click (function () {
$.getjson (' Test.js ', function (data) {
$ (' #resText '). empty ();
var html = ';
$.each (data, function (Commentindex, comment) {
HTML + = '

' + comment[' username ' + ':

' + comment[' content ' + '

';
})
$ (' #resText '). HTML (HTML);
})
})
})

All you need to do is store the data in a properly formatted. json or. js file. The following is the JSON-formatted data transmitted by the example

The code is as follows Copy Code


[
{
"username": "Zhang San",
"Content": "Sofa."
},
{
"username": "John Doe",
"Content": "Bench."
},
{
"username": "Harry",
"Content": "Floor."
}
]

The JSON data mentioned above is fixed, how do we return the JSON data with PHP?


PHP Output JSON Format method

Page, add

Header (' Content-type:text/json ');

This header is to inform this file output type is JSON, this form we see most is the verification code--php output to verify the picture, sometimes php can output CSS files, JS files and so do some interesting things. Okay, let's test it.

The code is as follows Copy Code

< PHP
Header (' Content-type:text/json ');

$fruits = Array (
"Fruits" = Array ("A" and "Orange", "B" and "Banana", "c" = "apple"),
"Numbers" = = Array (1, 2, 3, 4, 5, 6),
"holes" = = Array ("First", 5 = "Second", "third")
);
echo Json_encode ($fruits);
?>

Instance

Generate JSON format for data read from database

The code is as follows Copy Code




Tutorial provided by the first PHP network--generating JSON format for data read from the database






Note the structural differences between the two methods of generating an array of objects


Echo '

Fayi

';
Suppose the following array is generated from the data we read from the database
$jarr =array (' Total ' =>239, ' Row ' =>array (
Array (' Code ' = ' 001 ', ' name ' = ' China ', ' addr ' = ' Address one ', ' col4 ' = ' col4 data '),
Array (' Code ' = ' 002 ', ' name ' = = ' Name 2 ', ' addr ' = ' Address ', ' col4 ' = ' col4 data '),
)
);
Law One:
$jobj =new Stdclass ();//Instantiate Stdclass, which is an empty class built into PHP that can be used to pass data, since the Json_decode data is stored as an array of objects,
So we have to store the data in the object when we build it.
foreach ($jarr as $key = = $value) {
$jobj $key = $value;
}
Print_r ($jobj);//Print the object after passing the property
Echo ' uses $jobj->row[0][' code '] to output array elements: '. $jobj->row[0][' code '. '
';
Echo ' encoded JSON string: '. Json_encode ($jobj). '
';//print the encoded JSON string

Law II:
Echo ';
Echo '

Law II

';
Echo ' encoded JSON string: ';
echo $str =json_encode ($jarr);//JSON encoding of the array
Echo '
';
$arr =json_decode ($STR);//JSON decoding
Print_r ($arr);//print decoded array, data stored in object array
Echo ' uses the $arr->row[0]->code output array element: '. $arr->row[0]->code;

?>



http://www.bkjia.com/PHPjc/628765.html www.bkjia.com true http://www.bkjia.com/PHPjc/628765.html techarticle The data in JSON format is the data that we always use in the development of the application, such as the use of the JSON data for calling to or with the API, and how PHP returns the data in JSON format to ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.