How does PHP return json data to jquery?

Source: Internet
Author: User

Json-format data is the data we always use in application development. json data is used when you submit data with jquery or when you submit data with APIs, so how does PHP return json data to jquery? I will introduce it to you.

To operate json data in jquery, we directly $. parseJSON (returnString ).

Example

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 + = '<div class = "comment"> })
('{Restext'{.html (html );
})
})
})

What you need to do is to store the data in a. json or. js file in the correct format. The following is an example of the data transmitted in json format.

The Code is as follows: Copy code


[
{
"Username": "Zhang San ",
"Content": "sofa ."
},
{
"Username": "Li Si ",
"Content": "bench ."
},
{
"Username": "Wang Wu ",
"Content": "floor ."
}
]

The json data mentioned above is fixed. How can we return json data using php?


Php output JSON format Method

Add

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

This header indicates that the output type of this file is json. The most common form we see is the verification code-php output verification image. Sometimes php can output css files, js files and other 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" => "orange", "B" => "banana", "c" => "apple "),
"Numbers" => array (1, 2, 3, 4, 5, 6 ),
"Holes" => array ("first", 5 => "second", "third ")
);
Echo json_encode ($ fruits );
?>

Instance

The data read from the database is in json format.

The Code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> tutorial provided by the first php Network -- generate json format for data read from the database </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type = "text/javascript"/> </script>
<Script language = javascript>
</Script>
</Head>
<Body>
<Pre>
<H1> pay attention to the difference in structure between the object arrays generated by the two methods <? Php
Echo '// Assume that the following array is generated based on the data we read from the database.
$ Jarr = array ('Total' => 239, 'row' => array (
Array ('code' => '001', 'name' => 'China', 'addr '=> 'address 11 ', 'col4' => 'col4 data '),
Array ('code' => '002 ', 'name' => 'name 2', 'addr' => 'address 12 ', 'col4' => 'col4 data '),
)
);
// Method 1:
$ Jobj = new stdclass (); // instantiate stdclass, which is an empty class built in php and can be used to transmit data. Because the data after json_decode is stored as an object array,
// Therefore, we need to store the data in the object during generation.
Foreach ($ jarr as $ key => $ value ){
$ Jobj-> $ key = $ value;
}
Print_r ($ jobj); // print the object after passing the attribute
Echo 'use $ jobj-> row [0] ['code'] to output array elements :'. $ jobj-> row [0] ['code']. '<br> ';
Echo 'encoded json string: '. json_encode ($ jobj).' <br> '; // print the encoded json string

// Method 2:
Echo 'Echo 'Echo 'encoded json string :';
Echo $ str = json_encode ($ jarr); // encode the array in json format.
Echo '<br> ';
$ Arr = json_decode ($ str); // perform json decoding.
Print_r ($ arr); // print the decoded array. The data is stored in the object array.
Echo 'use $ arr-> row [0]-> code output array element:'. $ arr-> row [0]-> code;

?>


</Body>
</Html>

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.