The json_encode () function is a function in php that can convert arrays to json format. However, the json_encode function is not problematic in English, if there is any problem with the Chinese character or encoding, it will become null after the Chinese character is converted... the json_encode () function is a function in php that can convert arrays to json format. However, the json_encode function is not problematic in English, if there is any problem with the Chinese character or encoding, it will become null after the Chinese character is converted, next, I will introduce the php return json data application and some solutions to the conversion problems.
Json_encode () function usage.
echo json_encode(array('a'=>'bbbb','c'=>'ddddd');
In this way, a standard json format data is generated.
'; $ 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:
{"UserKeyGetResponse": {"RequestName": "Response", "api_key_value": "response"}, "error_response": {"code": "NO_ERROR", "msg ": "system parameter retrieved "}}
You can write the result as an array:
$ 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
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 _ MSG_MISS ', 'MSG' => 'message does not exist'); 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.
Link to this article:
Add to favorites ^ please keep the tutorial address.