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.