Data conversion in js format is a common method for data transmission. For example, ajax often uses to convert data to json and then convert it back. Let's look at an example.
The Code is as follows: |
Copy code |
Function array_to_json ($ array ){ If (! Is_array ($ array )){ Return false; } $ Associative = count (array_diff (array_keys ($ array), array_keys ($ array )))); If ($ associative ){ $ Construct = array (); Foreach ($ array as $ key => $ value ){
// We first copy each key/value pair into a staging array, // Formatting each key and value properly as we go.
// Format the key: If (is_numeric ($ key )){ $ Key = "key _ $ key "; } $ Key = ". addslashes ($ key ).""";
// Format the value: If (is_array ($ value )){ $ Value = array_to_json ($ value ); } Else if (! Is_numeric ($ value) | is_string ($ value )){ $ Value = ". addslashes ($ value )."""; }
// Add to staging array: $ Construct [] = "$ key: $ value "; } // Then we collapse the staging array into the JSON form: $ Result = "{". implode (",", $ construct )."}"; } Else {// If the array is a vector (not associative ):
$ Construct = array (); Foreach ($ array as $ value ){
// Format the value: If (is_array ($ value )){ $ Value = array_to_json ($ value ); } Else if (! Is_numeric ($ value) | is_string ($ value )){ $ Value = ". addslashes ($ value )."""; }
// Add to staging array: $ Construct [] = $ value; } // Then we collapse the staging array into the JSON form: $ Result = "[". implode (",", $ construct). "]"; } Return $ result; }
|
You can try this and change json_encode to the above function to see if it is normal.
The Code is as follows: |
Copy code |
If ($ _ GET ['enabs'] = 'OK '){ Echo json_encode (array ('A' => 'wang jin'); exit; } ?> <Script type = "text/javascript" src = "jquery. js"> </script> <script type = "text/javascript"> $ (Function (){ $. Get ("? Enews = OK ", function (result ){ Alert (result ); }); }); </Script> |
About json_encode in php
Json_encode () converts different types of PHP variables to the corresponding JSON string json_encode (mixed $ value [, int
$ Options = 0])
PHP 5.3.0
JSON_HEX_QUOT: Convert all double quotation marks (") to u0022.
// Instance code:
The Code is as follows: |
Copy code |
$ Data = '"'; Echo json_encode ($ data );//""" Echo json_encode ($ data, JSON_HEX_QUOT ); |
// "U0022" ■ JSON_HEX_TAG: Convert all greater than signs (>) to u003E
All minor signs (<) are converted to u003C.
JSON_HEX_AMP: Convert all ampersand (&) to u0026.
JSON_HEX_APOS: converts all single quotes (') to u0027.
JSON_FORCE_OBJECT: When the value is not associated with an array, the output result is a JSON object. When the receiver requires that the data be an object and the value is an empty array
.
// Instance code:
The Code is as follows: |
Copy code |
$ Data = array (); Echo json_encode ($ data); // [] Echo json_encode ($ data, JSON_FORCE_OBJECT );//{} |
PHP 5.3.3
JSON_NUMERIC_CHECK: Encodes numeric strings as numbers.
PHP 5.4.0
JSON_BIGINT_AS_STRING: Encodes large integers as their original string value. Available since PHP
5.4.0.
JSON_PRETTY_PRINT: Use whitespace in returned data to format it. Available since PHP 5.4.0.
JSON_UNESCAPED_SLASHES: Don't escape/. Available since PHP 5.4.0.
JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (default is to escape as uXXXX ).
Available since PHP 5.4.0.