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, how does PHP return json data to jq?
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 ).
The instance code is as follows:
- $ (Function (){
- $ ('# Send'). click (function (){
- $. GetJSON ('test. js', function (data ){
- $ ('# ResText'). emptyempty ();
- 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 the correct format. json or. js file. The following is the json format data transmitted in the example.
The instance code is as follows:
- [
- {
- "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 the header ('content-type: text/json') to the page. this header indicates that the output type of this file is json, what we see most in this form is the verification code-php output verification image. sometimes php can output css files and js files to do some interesting things. okay. let's test it.
The instance code is as follows:
- <? 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 );
- ?>
The data read from the database is in json format.
The instance code is as follows:
-
-
- Tutorial provided by the first php network-generate the data read by the database in json format
- Script
- Script
-
-
-
- Note the differences in structure between the object arrays generated by the two methods.
- Echo 'method 1 ';
- // 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'].'
';
- Echo 'encoded json string: '. json_encode ($ jobj ).'
'; // Print the encoded json string
- // Method 2:
- Echo '';
- Echo 'method 2 ';
- Echo 'encoded json string :';
- Echo $ str = json_encode ($ jarr); // Encode the array in json format.
- Echo'
';
- $ 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;
- ?>
-
-
-