Home  >  Article  >  Backend Development  >  The php json_encode() function returns the json data instance code, json_encodejson_PHP tutorial

The php json_encode() function returns the json data instance code, json_encodejson_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:17:351031browse

php json_encode() function returns json data instance code, json_encodejson

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

<&#63;php

//需要执行的SQL语句
//单条
$sql="select id,name from tbl_user where id=1";
//多条数据
//$sql="select id,name from tbl_user";

//调用conn.php文件进行数据库操作
require('Conn.php');

//提示操作成功信息,注意:$result存在于conn.php文件中,被调用出来
if($result)
{

// $array=mysql_fetch_array($result,MYSQL_ASSOC);


/*数据集

$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));

*/

/*单条数据*/

$row=mysql_fetch_row($result,MYSQL_ASSOC);

echo json_encode(array('jsonObj'=>$row));
}

mysql_free_result($result);
//释放结果
mysql_close();
//关闭连接

&#63;>

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:

The code is as follows

Copy code The code is as follows:

{
"UserKeyGetResponse":
{"RequestName":"e99e6d63e8c712d7699f52978a","api_key_value":"41954dd9b1cb6a95802eab6810"},
"error_response":
{"code":"NO_ERROR(www.jb51.net)","msg":"Get system parameters successfully"}
}

The result can be written in the form of an array like this:
Copy code 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' => 'Get system parameters successfully'));

Code

Copy 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 does not exist');
echo JSON($array);

The result is:

{"code":"ERROR_MSG_MISS","msg":"Message does not exist"}
The client can parse the 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.

Teaching PHP json code examples

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

php code for outputting JSON data

Dear, you didn’t execute it in the environment at all. You need to compile it with php+apache

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/891103.htmlTechArticlephp json_encode() function returns json data instance code, json_encodejson json_encode() function usage. echo json_encode(array('a'='bbbb','c'='ddddd'); This will generate a standard...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn