Php json_encode () function returns the json data instance code, json_encodejson

Source: Internet
Author: User
Tags php json

Php json_encode () function returns the json data instance code, json_encodejson

Json_encode () function usage.

Echo json_encode (array ('A' => 'bbbbb', 'c' => 'ddddd ');

In this way, a standard json format data is generated.

The Code is as follows:

<? 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']. '</br>'; $ 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?>

The 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:

The Code is as follows:
Copy codeThe Code is as follows:
{
"UserKeyGetResponse ":
{"RequestName": "e99e6d63e8c712d7699f52978a", "api_key_value": "41954dd9b1cb6a95802eab6810 "},
"Error_response ":
{"Code": "NO_ERROR (www.jb51.net)", "msg": "system parameter retrieved "}
}

You can write the result as an array:
Copy codeThe 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 '=> 'System parameter retrieved successfully '));

Code

Copy codeThe 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 nonexistent ');
Echo JSON ($ array );

Result:

{"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 can display Chinese characters directly. Of course, it is okay to display the hexadecimal encoding.


Example of how to use json code in PHP

Use the json_decode function to decrypt data .. The JSON function used by PHP is: json_encode ($ PHPcode );
The JSON parsing function of PHP is: json_decode ($ JSONcode); therefore, there are many JSON forms, and different forms are different after PHP interprets them. // Form 1: It is completely in the form of an object. This form of data is also called a related array in Javascript. What is different from a general array is that, it can be accessed through string indexing (using "[]" or ". $ json = '{"item1": {"item11": {"n": "chenling", "m": "llll "}, "sex": "male", "age": "25"}, "item2": {"item21": "ling", "sex": "female ", "age": "24"} '; $ J = json_decode ($ json); print_r ($ J ); let's take a look at this article on php json data operations from the php beginner's portal: www.phpnewer.com/index.php/Cjwt/detail/id/147.

Php code for outputting JSON data

You didn't execute it in the environment. php + apache compilation is required.

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.