JSON Data Format Introduction _json

Source: Internet
Author: User
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 ("\\\");
Case ' \ ':
Sb.append ("\\\\");
Case '/':
Sb.append ("\\/");
Case ' \b ':
Sb.append ("\\b");
Case ' \f ':
Sb.append ("\\f");
Case ' \ n ':
Sb.append ("\\n");
Case ' \ R ':
Sb.append ("\ r");
Case ' t ':
Sb.append ("\\t");
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.

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.