The PHP json_encode () function returns the JSON data instance code, json_encodejson_php tutorial

Source: Internet
Author: User

The PHP json_encode () function returns the JSON data instance code, Json_encodejson


Json_encode () function usage.

echo json_encode (Array (' a ' = ' = ' bbbb ', ' c ' = ' ddddd ');

This will generate a standard JSON-formatted data

The code is as follows

<?php//need to execute SQL statement//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 operation require (' conn.php ');//Prompt Operation success Information, note: $result exists in the conn.php file, is called out if ($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 ']. '
'; $users [$i]= $row; $i + +;} echo json_encode (Array (' dataList ' + $users)), *//* single data */$row =mysql_fetch_row ($result, MYSQL_ASSOC); Echo Json_ Encode (Array (' jsonobj ' = $row));} Mysql_free_result ($result);//release result mysql_close ();//close Connection?>

This is the database that generated the 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:

The code is as follows
Copy the Code code as follows:
{
"Userkeygetresponse":
{"Requestname": "e99e6d63e8c712d7699f52978a", "Api_key_value": "41954dd9b1cb6a95802eab6810"},
"Error_response":
{"Code": "No_error (www.jb51.net)", "MSG": "Get System parameters succeeded"}
}

You can write the result in an array form like this:
Copy the Code code 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 succeeded '));

Code

Copy the Code 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 not present ');
echo JSON ($array);

The result 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.

So much the more we show the direct is the Chinese, of course, the 16 binary encoding is not a problem oh.


Teach PHP to take JSON code example

Use the Json_decode function to decrypt the data: PHP application JSON functions are: Json_encode ($PHPcode);
PHP parsing JSON functions are: Json_decode ($JSONcode), so there are many forms of JSON, different forms in PHP after the form of interpretation is also different. Form 1: Exactly the form of the object, this form of data in JavaScript is also called the relevant array, unlike a general array, it can be indexed by the string to access (with "[]" or "." to represent the hierarchy) $json = ' {"" item1 ": {" Item11 ": {" n ":" Chenling "," M ":" Llll "}," Sex ":" Male "," age ":" + "}," item2 ": {" Item21 ":" Ling "," Sex ":" Female "," Age ":" 24 "}} '; $J =json_decode ($json); Print_r ($J); Take a look at the PHP novice web This article on PHP operation JSON data: www.phpnewer.com/index.php/Cjwt/detail/id/147

Code for PHP output JSON data

Pro, you are not in the environment to execute AH, need to php+apache compiled

http://www.bkjia.com/PHPjc/891103.html www.bkjia.com true http://www.bkjia.com/PHPjc/891103.html techarticle the PHP json_encode () function returns the JSON data instance code, Json_encodejson the Json_encode () function usage. echo json_encode (Array (' a ' = ' bbbb ', ' c ' = ' ddddd '), which generates a standard ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.