Introduction to JSON Data Format

Source: Internet
Author: User

1. Basics
This article is more in-depth, so I don't need to draw any more, but the following code demonstrates how to process string data in JSON and save it for future reference:
Zt from: https://www.ibm.com/developerworks/cn/web/wa-lo-json/
When you encode a String object in JSON format, you only need to process special characters. In addition, the string must be (") rather:
Copy codeThe Code is as follows:
Static String string2Json (String s ){
StringBuilder sb = new StringBuilder (s. length () + 20 );
Sb. append ('\"');
For (int I = 0; I <s. length (); I ++ ){
Char c = s. charAt (I );
Switch (c ){
Case '\"':
Sb. append ("\\\"");
Break;
Case '\\':
Sb. append ("\\\\");
Break;
Case '/':
Sb. append ("\\/");
Break;
Case '\ B ':
Sb. append ("\ B ");
Break;
Case '\ F ':
Sb. append ("\ f ");
Break;
Case '\ N ':
Sb. append ("\ n ");
Break;
Case '\ R ':
Sb. append ("\ r ");
Break;
Case '\ t ':
Sb. append ("\ t ");
Break;
Default:
Sb. append (c );
}
}
Sb. append ('\"');
Return sb. toString ();
}

2. Applications in PHP.
In PHP, json_encode and json_decode are used for JSON encapsulation and disassembly.
Encapsulate string data: json_encode is added to both ends of the source string in the output result. For example, if the object content is (abc), the result is ("abc ")
Encapsulate data of the stdClass object type. For example, if a "value" member is a string, the result is {"value": "\ u5e7f \ u4e1c \ u4f53 \ u80b2, CCTV5 "}
3. Why write this.
In the ZenTaoPHP framework, if the request type is "json", the output result is encapsulated in JSON format. However, when analyzing the output information, it is found that it is not a standard JSON format.
After further research, it is found that ZenTaoPHP encapsulates a portion of the data in JSON format, and the output is similar to: "{\" value \": \ "\ u5e7f \ u4e1c \ u4f53.

Related Article

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.