This article describes the use of PHP return JSON data functions, shared for everyone to reference. Here's how:
Json_encode () function usage:
echo json_encode (array(' a ' = ' = ' bbbb ', ' c ' = ' ddddd ');
This will generate a standard JSON-formatted data
<?PHP//SQL statements to be executed// single$sql= "Select Id,name from Tbl_user where id=1";//multiple data//$sql = "Select Id,name from Tbl_user";//Call conn.php file for database operationrequire(' conn.php ');//Prompt Operation success Information, note: $result exists in the conn.php file, is called outif($result){//$array =mysql_fetch_array ($result, MYSQL_ASSOC); /*Data Set $users =array (), $i =0, while ($row =mysql_fetch_array ($result, MYSQL_ASSOC)) {echo $row [' id ']. ' -----------'. $row [' name ']. ' </br> '; $users [$i]= $row; $i + +; } Echo Json_encode (Array (' dataList ' = $users)); */ /*Single data*/ $row=Mysql_fetch_row($result,MYSQL_ASSOC); EchoJson_encode (Array(' jsonobj ' =$row));}Mysql_free_result($result);//Release ResultsMysql_close();//Close Connection?>
Above is the database to generate JSON data
Single data: {"Jsonobj": {"id": "1", "name": "LMW"}}
Multiple data: {"DataList": [{"id": "1", "name": "LMW"},{"id": "2", "Name": "XXJ"},{"id": "3", "Name": "Xxxj"}]}
Now in many cases, we need the program to return a JSON-formatted result, such as:
{"userkeygetresponse":{"requestname": "e99e6d63e8c712d7699f52978a", "Api_key_value": " 41954dd9b1cb6a95802eab6810 "}," Error_response ":{" code ":" No_error "," MSG ":" Get system parameters succeeded "}} The result can be written as an array of the form:$responarrayarray$api _request_name$api _key_value Array(' Code ' = ' no_error ', ' msg ' = ' = ' Get system parameters succeeded ');
The code is as follows:
functionArrayrecursive (&$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 does not exist ');EchoJSON ($array);
The result of the operation is:
{"Code": "Error_msg_miss", "MSG": "Message does not exist"}
The client can parse the result, and of course the error code should be replaced with a number.
This is much better. We are showing the direct Chinese, of course, the 16 encoding is not a problem.
PS: About JSON operation, here again for you to recommend a few more useful JSON online tool for everyone to refer to the use:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online formatting/landscaping/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat
Online JSON compression/escape tool:
Http://tools.jb51.net/code/json_yasuo_trans
C Language Style/html/css/json code formatting beautification tool:
Http://tools.jb51.net/code/ccode_html_css_json
PHP returns JSON data function instance _php tips _ Scripting House