1, the Foundation
This article is more in-depth, so I do not have to gild the lily, but the following code is a good demonstration of how JSON processing string data, saved 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 handle special characters. In addition, the string must be represented by (") rather than ('):
Copy Code code 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, application in PHP.
PHP uses Json_encode and json_decode for JSON encapsulation and disassembly.
Encapsulates string data: Json_encode in the output result, add (") at both ends of the source string, such as the object content (ABC), and the result (" ABC ")
Encapsulates Stdclass object type data: For example, a "value" member content is a string, and 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 is encapsulated in JSON format, but when the output information is parsed, it is not a standard JSON format.
After the drill, we found that some of the data zentaophp two JSON encapsulation, the output is similar to: "{\" value\ ": \" \\u5e7f\\u4e1c\\u4f53\ "}" results.