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