PHP generated JSON-related issues

Source: Internet
Author: User
Tags urlencode
A few questions about PHP generating JSON data.

foreach($re as $key=>$value){    $dd['code'][0]->$key=$value;    }$ff = json_encode($dd);print_r($ff);// 输出{"code":[{"0":{"key":"value"},"1":{"key":"value"}}]}

How would you like to add a bit of content before code? For example "url":"www.com" , make the generated JSON like this.

{"url":"www.com", "code":{"0":{"key":"value"},"1":{"key":"value"}}}}

Or is the output of the Chinese all become so \u8f8a\u67e5\u538d , how to output into Chinese?

Google has not found a half-day approach to find the basic all this.

$aa = urlencode($dd);echo urldecode(json_encode($aa));

But this not only can not output Chinese, even the output is not ...
Var_dump ($AA) output is NULL , this can do is good ah.

============= additional =================
There's a problem with that.

{    "url":"www.com",    "0":    {        "code":        {            "0":{"key":"value"},            "1":{"key":"value"}        }    }}

The resulting JSON will turn out this way, how do you get rid of this number?

================= two Chase ===================

$aa = array("url"=>"www.com");json_encode(array($aa,$dd['code']), JSON_UNESCAPED_UNICODE);

This will do what you want, but there's a problem.
How do you get a serial number from MySQL data?

Reply content:

A few questions about PHP generating JSON data.

foreach($re as $key=>$value){    $dd['code'][0]->$key=$value;    }$ff = json_encode($dd);print_r($ff);// 输出{"code":[{"0":{"key":"value"},"1":{"key":"value"}}]}

How would you like to add a bit of content before code? For example "url":"www.com" , make the generated JSON like this.

{"url":"www.com", "code":{"0":{"key":"value"},"1":{"key":"value"}}}}

Or is the output of the Chinese all become so \u8f8a\u67e5\u538d , how to output into Chinese?

Google has not found a half-day approach to find the basic all this.

$aa = urlencode($dd);echo urldecode(json_encode($aa));

But this not only can not output Chinese, even the output is not ...
Var_dump ($AA) output is NULL , this can do is good ah.

============= additional =================
There's a problem with that.

{    "url":"www.com",    "0":    {        "code":        {            "0":{"key":"value"},            "1":{"key":"value"}        }    }}

The resulting JSON will turn out this way, how do you get rid of this number?

================= two Chase ===================

$aa = array("url"=>"www.com");json_encode(array($aa,$dd['code']), JSON_UNESCAPED_UNICODE);

This will do what you want, but there's a problem.
How do you get a serial number from MySQL data?

There are two questions for you:

1, how to append content?

JSON and array correspond, just as you would append to the data:

$url = 'www.com';$code = array(    array('key', 'value');    array('key', 'value');    array('key', 'value'););echo json_encode(array('url'=>$url, 'code'=>$code));

2, how not garbled?

After php5.4 JSON has a new option JSONunescapedUNICODE:

echo json_encode(array('url'=>$url, 'code'=>$code), JSON_UNESCAPED_UNICODE);

Let JSON know more Chinese

To code add content to the front, you can json add a value before the array you want to convert to. -_-

$dd['url'] = 'www.xx.com';

The PHP version starts at 5.4 JSON_UNESCAPED_UNICODE , and the parameter constants are used to convert the characters to unicode codes, and refer to the manual.

If the PHP version is less than 5.4, refer to the following methods:

function json_encode_utf8($value) {    $jsons = json_encode($value);    return preg_replace(        "#\\\u([0-9a-f]{4}+)#ie",        "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))",        $jsons    );}

\u8f8a\u67e5\u538dIn itself is Chinese ah, this is handed to the front-end processing.

Question 1:

$dd['url'] = 'www.com';$dd['code'] = array();

Question 2:
See 1 floor, that is JS in the recommended use of Chinese characters, and file encoding independent. JS in direct use

  • 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.