The phpjson_encode () function returns the json data instance code, json_encodejson. The phpjson_encode () function returns the json data instance code, which is used by the json_encodejsonjson_encode () function. Echojson_encode (array (abbbb, cddddd); this generates a standard php json_encode () function that returns the json data instance code, json_encodejson
Json_encode () function usage.
Echo json_encode (array ('a' => 'bbbbb', 'C' => 'ddddd ');
In this way, a standard json format data is generated.
The code is as follows:
<? Php // SQL statement to be executed // Single $ SQL = "select id, name from tbl_user where id = 1 "; // multiple data records // $ SQL = "select id, name from tbl_user"; // call conn. php file for database operation require ('Conn. php '); // The message indicating successful operation is displayed. note: $ result exists in conn. in the php file, if ($ result) {// $ array = mysql_fetch_array ($ result, MYSQL_ASSOC);/* dataset $ users = array (); $ I = 0; while ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC) {echo $ row ['id']. '-----------'. $ row ['name'].'
'; $ Users [$ I] = $ row; $ I ++;} echo json_encode (array ('datalist' => $ users )); * // * single data entry */$ row = mysql_fetch_row ($ result, MYSQL_ASSOC); echo json_encode (array ('jsonobj '=> $ row ));} mysql_free_result ($ result); // release result mysql_close (); // close the connection?>
The above is the json data generated by the database.
Single data entry: {"jsonObj": {"id": "1", "name": "lmw "}}
Multiple data entries: {"dataList": [{"id": "1", "name": "lmw" },{ "id": "2 ", "name": "xxj" },{ "id": "3", "name": "xxxj"}]}
In many cases, the program needs to return a result in Json format, for example:
The code is as follows:
The code is as follows:
{
"UserKeyGetResponse ":
{"RequestName": "e99e6d63e8c712d7699f52978a", "api_key_value": "41954dd9b1cb6a95802eab6810 "},
"Error_response ":
{"Code": "NO_ERROR (www.jb51.net)", "msg": "system parameter retrieved "}
}
You can write the result as an array:
The code is as follows:
$ Respon = array ('userkeygetresponse' => array ('requestname' => $ api_request_name, 'api _ key_value '=> $ api_key_value ),
'Error _ response' => array ('code' => 'No _ error', 'MSG '=> 'system parameter Retrieved successfully '));
Code
The code is as follows:
Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false)
{
Static $ recursive_counter = 0;
If (++ $ recursive_counter> 1000 ){
Die ('possible deep recursion attack ');
}
Foreach ($ array as $ key => $ value ){
If (is_array ($ value )){
ArrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also );
} Else {
$ Array [$ key] = $ function ($ value );
}
If ($ apply_to_keys_also & is_string ($ key )){
$ New_key = $ function ($ key );
If ($ new_key! = $ Key ){
$ Array [$ new_key] = $ array [$ key];
Unset ($ array [$ key]);
}
}
}
$ Recursive_counter --;
}
G:
$ Error_respon = array ('code' => 'Error _ MSG_MISS ', 'MSG' => 'message nonexistent ');
Echo JSON ($ array );
Result:
{"Code": "ERROR_MSG_MISS", "msg": "The message does not exist "}
The client can parse this result. of course, the error code should be replaced by a number.
This is much better. we can display Chinese characters directly. of course, it is okay to display the hexadecimal encoding.
Example of how to use json code in PHP
Use the json_decode function to decrypt data .. The JSON function used by PHP is: json_encode ($ PHPcode );
The JSON parsing function of PHP is: json_decode ($ JSONcode); therefore, there are many JSON forms, and different forms are different after PHP interprets them. // Form 1: it is completely in the form of an object. this form of data is also called a related array in Javascript. What is different from a general array is that, it can be accessed through string indexing (using "[]" or ". $ json = '{"item1": {"item11": {"n": "chenling", "m": "llll "}, "sex": "male", "age": "25"}, "item2": {"item21": "ling", "sex": "female ", "age": "24"} '; $ J = json_decode ($ json); print_r ($ J ); let's take a look at this article on php json data operations from the php beginner's Portal: www.phpnewer.com/index.php/Cjwt/detail/id/147.
Php code for outputting JSON data
You didn't execute it in the environment. php + apache compilation is required.
The http://www.bkjia.com/PHPjc/891103.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/891103.htmlTechArticlephp json_encode () function returns the json data instance code, json_encodejson json_encode () function usage. Echo json_encode (array ('a' = 'bbbbbb', 'C' = 'ddddd '); then a standard...