Php return json data function example

Source: Internet
Author: User
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.

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.