How to generate nested JSON in PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP generates nested JSON. PHP generates nested JSON ({aa: [{Id: 0, title: title, },{ Id: 1, title: title,}], bb: [{...}, {...}]}) how does PHP generate such nested JSON ------ solution ------------ PHP generates nested JSON

({

"Aa ":[

{

"Id": "0 ",

"Title": "title ",

},

{

"Id": "1 ",

"Title": "title ",

}

],

"Bb ":[

{

...

},

{

....

}

]

})

How does PHP generate such nested JSON

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

/** Format Json data

* @ Param Mixed $ data

* @ Param String $ indent character. the default value is 4 spaces.

* @ Return JSON

*/

Function jsonFormat ($ data, $ indent = null ){

// Recursive urlencode operation on each element in the array to protect Chinese characters

Array_pai_recursive ($ data, 'jsonformatprotect ');

// Json encode

$ Data = json_encode ($ data );

// Urlencode the content in urldecode

$ 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;

}

/** Urlencode the array element

* @ 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 (

'A' => array (

Array (

'Id' => 0,

'Title' => 'title'

), Array ('id' => 1, 'title' => 'title'),), '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"}]}

Dimensions ({aa: [{Id: 0, title: title,}, {Id: 1, title: title,}], bb :[{...}, {...}]}) how does PHP generate such 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.