Php returns the json data function instance, and php returns the json instance. Php returns a json data function instance. php returns a json instance. This article describes the usage of the json data function returned by php. The specific method is as follows: json_encode () function. php returns the json data function instance, and php returns the json instance.
This example describes how to use the json data function returned by php. The specific method is as follows:
Json_encode () function usage:
echo json_encode(array('a'=>'bbbb','c'=>'ddddd');
In this way, a standard json format data is generated.
<? 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?>
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:
{"UserKeyGetResponse": {"RequestName": "Response", "api_key_value": "response"}, "error_response": {"code": "NO_ERROR", "msg ": "system parameter retrieved"} you can write the result as an array: $ 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 '));
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_k Ey = $ 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 );
The running result is:
{"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 now display Chinese characters directly. of course, it is okay to display the hexadecimal encoding.
I hope this article will help you with PHP programming.
How does PHP process the json data returned by the backend?
You are sending an ajax request with jquery! You can use $. each (msg, function (item, index ){
// Item indicates each object!
// Index indicates the index
});
Php implements http crawling. if you want to implement the fetchurl function in sae locally, you can return json format data.
Refer to the principle of distributed http page capturing by sae/
Reference address: e.sae.sina.com.cn/...pstore
Examples in this article describe how php returns json data functions and share them with you for your reference. The specific method is as follows: json_encode () letter...