json_encode() function usage.
echo json_encode(array('a'=>'bbbb','c'=>'ddddd');
This will generate a standard json format data
The code is as follows Copy the code
//SQL statement to be executed
//Single piece
$sql="select id,name from tbl_user where id=1";
//Multiple pieces of data
//$sql ="select id,name from tbl_user";
//Call the conn.php file to perform database operations
require('Conn.php');
//Prompt operation success message, note: $result exists in the conn.php file and is called out
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 piece of data*/
$row=mysql_fetch_row($result,MYSQL_ASSOC);
echo json_encode(array('jsonObj'=>$row));
}
mysql_free_result($result);
//Release the result
mysql_close();
//Close the connection
?>
The above is the json data generated by the database
Single piece of data: {"jsonObj":{"id":"1","name":"lmw"}}
Multiple pieces of data: {"dataList":[{"id":"1","name":"lmw"},{"id":"2","name":"xxj"},{ "id":"3","name":"xxxj"}]}
In many cases now, we need the program to return a result in Json format, such as:
Copy the code as follows
{
"UserKeyGetResponse":
{"RequestName":"e99e6d63e8c712d7699f52978a","api_key_value":"41954dd9b1cb6a95802eab6810"},
" error_response":
{ "code":"NO_ERROR(www.111cn.net)","msg":"Get system parameters successfully"}
}
The result can be written in the form of an array like this:
$respon = array('UserKeyGetResponse' => array('RequestName' => $api_request_name, 'api_key_value' => $api_key_value),
'error_response' => array('code' => 'NO_ERROR', 'msg' => 'Get system parameters successfully'));
Code
Copy the code 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 does not exist');
echo JSON($array);
The result is:
{"code":"ERROR_MSG_MISS","msg":"Message does not exist"}
The client can parse this result. Of course, the error code must be replaced by a number.
This is much better. What we display is Chinese directly. Of course, there is no problem in displaying the hexadecimal encoding.
from:http://www.111cn.net/phper/php/62544.htm
You are using jquery to send the ajax request! You can use $.each(msg,function(item,index){
//item represents each object!
//index represents index
});
Use the json_decode function to decrypt the data. . The function for PHP to apply JSON is: json_encode($PHPcode);
The function for PHP to parse JSON is: json_decode($JSONcode); so there are many forms of JSON, and different forms have different forms after being interpreted by PHP. . //Form 1: It is completely in the form of an object. This form of data is also called a related array in Javascript. Different from ordinary arrays, it can be accessed through a string as an index (use "[]" or "." to represent the level) $json='{"item1":{"item11":{"n":"chenling", "m":"llll"},"sex":"male","age":"25 "},"item2": {"item21":"ling","sex":"女","age":"24"}}'; $J=json_decode($json); print_r($J); Take a look at this article about php operating json data from the PHP Beginner's Guide: www.phpnewer.com/index.php/Cjwt/detail/id/147