PHP generates nested JSON solutions _php Tutorial

Source: Internet
Author: User
PHP generates nested JSON

({

"AA": [

{

"Id": "0",

"title": "caption",

},

{

"Id": "1",

"title": "caption",

}

],

"BB": [

{

...

},

{

....

}

]

})

How PHP generates this nested JSON

------Solution--------------------

/** JSON data formatting

* @param Mixed $data data

* @param String $indent indent character, default 4 spaces

* @return JSON

*/

function Jsonformat ($data, $indent =null) {

UrlEncode each element recursively in an array, protecting Chinese characters

Array_walk_recursive ($data, ' jsonformatprotect ');

JSON encode

$data = Json_encode ($data);

UrlDecode the contents of the UrlEncode

$data = UrlDecode ($data);

Indent processing

$ret = ";

$pos = 0;

$length = strlen ($data);

$indent = Isset ($indent)? $indent: ";

$newline = "\ n";

$prevchar = ";

$outofquotes = true;

for ($i =0; $i <= $length; $i + +) {

$char = substr ($data, $i, 1);

if ($char = = ' "' && $prevchar! = ' \ \ ') {

$outofquotes =! $outofquotes;

}elseif (($char = = '} '------solution--------------------$char = = '] ') && $outofquotes) {

$ret. = $newline;

$pos--;

for ($j =0; $j < $pos; $j + +) {

$ret. = $indent;

}

}

$ret. = $char;

if ($char = = ', '------solution--------------------$char = = ' {'------solution--------------------$char = = ' [') && $ Outofquotes) {

$ret. = $newline;

if ($char = = ' {'------solution--------------------$char = = ' [') {

$pos + +;

}

for ($j =0; $j < $pos; $j + +) {

$ret. = $indent;

}

}

$prevchar = $char;

}

return $ret;

}

/** the elements of an array UrlEncode

* @param String $val

*/

Function Jsonformatprotect (& $val) {

if ($val!==true && $val!==false && $val!==null) {

$val = UrlEncode ($val);

}

}

Header (' Content-type:application/json;charset=utf8 ');

$arr = Array (

' AA ' = = Array (

Array

' Id ' = 0,

' Title ' = ' headline '

), array (' id ' = = 1, ' title ' + ' caption '), ' bb ' = = Array (' id ' = ' = ' + 2, ' title ' = ' title '), Array (' ID ' = 3, ' title ' = ' title '),); Echo Jsonformat ($arr); {"AA": [{"id": "0", "title": "title"}, {"id": "1", "title": "title"}], "BB": [{"id": "2", "title": "title"}, {"id": "3", "title": "title"}]}

http://www.bkjia.com/PHPjc/820425.html www.bkjia.com true http://www.bkjia.com/PHPjc/820425.html techarticle PHP generates nested JSON {AA: [{id:0, Title: Caption,}, {id:1, title: Title,}], bb:[{...}, {...}}) How does PHP generate this nested JSON------solution--------------...

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