1. Basics
This article is quite in-depth, so I don’t need to add any more details. The following code is a good demonstration of how JSON processes string data. Save it for future reference:
zt from:
https://www.ibm.com/developerworks/cn/web/wa-lo-json/ When encoding the String object into JSON format, you only need to handle the special characters. Can. In addition, strings must be represented by (") instead of ('):
static String string2Json(String s) {
StringBuilder sb = new StringBuilder(s.length() 20);
sb.append('"');
for (int i=0 ; ichar 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. Json_encode and json_decode are used in PHP to encapsulate and disassemble string type data: json_encode will be included in the output result. Add (") to the end, if the object content is (abc), the result is ("abc")
Encapsulates stdClass object type data: For example, if there is a "value" member whose content is a string, the result is: {"value" :"u5e7fu4e1cu4f53u80b2,CCTV5"}
3, why are you writing this.
In the ZenTaoPHP framework, if the request type is "json", the output result will be encapsulated in JSON format. However, when analyzing the output information, it is found that it is not in the standard JSON format. After further investigation, I found that ZenTaoPHP encapsulated some data twice in JSON, and the output was similar to: "{"value":"\u5e7f\u4e1c\u4f53"}".
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn