PHP format and output json data example

Source: Internet
Author: User
Tags json strlen

Php directly outputs json format

Php directly outputs the json format. Many new users have a misunderstanding that echo json_encode ($ data) is used to output json data. If yes, the output text is in json format rather than json data, the correct statement should be followed by one sentence:

The code is as follows: Copy code
<? Php
 
Header ('content-type: text/json'); // indicates the object receiving data. json data is output on this page;
$ Json = {"name": "yovae", "password": "12345"}; // Although this line of data is in json format, if there is no such sentence, it will not be processed as json data;
Echo $ json;
?>



Example

JSON data formatting function

Format JSON data in string form into indent form. Generally, the JSON string converted using json_encode is not indented. This method is much better.
Here, tab indentation is used by default. To change it to a space, replace the variable $ indentStr.

The code is as follows: Copy code
/**
* Indents a flat JSON string to make it more human-readable.
* @ Param string $ json The original JSON string to process.
* @ Return string Indented version of the original JSON string.
*/
Function indent ($ json ){

$ Result = '';
$ Pos = 0;
$ StrLen = strlen ($ json );
$ IndentStr = '';
$ NewLine = "\ n ";
$ PrevChar = '';
$ OutOfQuotes = true;

For ($ I = 0; $ I <= $ strLen; $ I ++ ){

// Grab the next character in the string.
$ Char = substr ($ json, $ I, 1 );
// Are we inside a quoted string?
If ($ char = '"' & $ prevChar! = '\\'){
$ OutOfQuotes =! $ OutOfQuotes;
// If this character is the end of an element,
// Output a new line and indent the next line.
} Else if ($ char = '}' | $ char = ']') & $ outOfQuotes ){
$ Result. = $ newLine;
$ Pos --;
For ($ j = 0; $ j <$ pos; $ j ++ ){
$ Result. = $ indentStr;
}
}
// Add the character to the result string.
$ Result. = $ char;
// If the last character was the beginning of an element,
// Output a new line and indent the next line.
If ($ char = ',' | $ char = '{' | $ char = '[') & $ outOfQuotes ){
$ Result. = $ newLine;
If ($ char = '{' | $ char = '['){
$ Pos ++;
}
For ($ j = 0; $ j <$ pos; $ j ++ ){
$ Result. = $ indentStr;
}
}
$ PrevChar = $ char;
}

Return $ result;

}



Well, the json database output in this way is very beautiful and formatted. Here I will not give an example. Please refer to it without your defense.

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.