Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ How to handle special characters in JSON     - RedHat Linux 5.5 installation process SVN Service Notes (Server)

- Source code to compile and install MySQL 5.7.9 (Database)

- Ubuntu Linux to create and increase the Swap partition tutorial (Linux)

- Windows 8.1 and Ubuntu 14.04 dual system uninstall Ubuntu Tutorial (Linux)

- MySQL concat function is SQL injection (Linux)

- JavaScript subarray Deduplication (Programming)

- To install the latest version of the EPEL on CentOS 5.x or 6.x (Linux)

- iOS9 new feature - stacked view UIStackView (Programming)

- HBase table data processing tab (Database)

- Linux iptables: basic principles and rules (Linux)

- SQL Server automatic backup script (Database)

- OS X CAOpenGLLayer how to enable OpenGL 3.2 core profile (Programming)

- Commentary Apache + Tomcat + JK implement Tomcat clustering and load (Server)

- Oracle Sql Loader tool has shown signs (Database)

- RHEL5 multipath configuration (Linux)

- Python data types summary (Programming)

- Linux Security Module (LSM) Introduction (Linux)

- How to track performance issues when using the Unity Game Development Android (Programming)

- Understand the security restore accidentally deleted critical system files (Linux)

- OpenSUSE GPG Comments (Linux)

  How to handle special characters in JSON
  Add Date : 2018-11-21      
  JSON is an effective format suitable for Ajax applications, because it allows to quickly convert JavaScript objects and string values. Because Ajax application is very suitable for the plain text is sent to the server program and the corresponding received plain text, compared to not generate text API, API can generate text will naturally be more desirable; moreover, JSON allows you to deal with native JavaScript object, no need to bother how to represent more of these objects.

XML can also provide similar benefits text area, but for the JavaScript object into XML several existing API JSON API does not mature; sometimes you need when creating and handling of JavaScript objects extreme caution to ensure that the process can be carried out session with the selected XML API collaboration. But for JSON, the situation is quite different: it can handle almost all possible types of objects, and will be returned to you a very good JSON data representation. Therefore, JSON greatest value is that it can really be JavaScript data format rather than as a JavaScript language processing.
All tips on using JavaScript objects that you have learned can be applied to the code without having to turn to how these objects into text and more bother.

Enter problem
JSON pass value, if there is a carriage return will be linked. We can use a regular to remove carriage returns:

$ Str = preg_replace ( " '([\ r \ n]) [\ s] +'", "", $ str);

// Do not have regular
$ Str = str_replace ( "\ n", "", $ str);

Turn out a string of no carriage returns troubled.

Incidentally, recording a PHP script filter:

< ? Php
// $ Document should contain an HTML document.
// This example will remove HTML tags, javascript codes
// And white space characters. Also some general
// HTML entity conversion to the corresponding text.
$ Search = array ( " '< script [^>] *?>. *? < / Script>' si", // remove javascript
" '< [\ / \!] *? [^ < >] *?>' Si", // remove HTML tags
" '([\ R \ n]) [\ s] +'", // remove whitespace characters
" '& (Quot | # 34);' i", // replace HTML entities
" '& (Amp | # 38);' i",
" '& (Lt | # 60);' i",
" '& (Gt | # 62);' i",
" '& (Nbsp | # 160);' i",
" '& (Iexcl | # 161);' i",
" '& (Cent | # 162);' i",
" '& (Pound | # 163);' i",
" '& (Copy | # 169);' i",
" '& # (\ D +);' e"); // run as PHP code
$ Replace = array ( "",
" ',
"\" ",
"< "
" ',
chr (161),
chr (162),
chr (163),
chr (169),
"Chr (\\ 1)");
$ Text = preg_replace ($ search, $ replace, $ document);
HTML special characters
After from the server in JSON format to pass data to the client, by JS displayed on the HTML page, there are some special characters can not be displayed directly, such as a backstage pass over the 'msg #' through JS displayed in an HTML page to display became msg #, not msg #, this is due to the content of the < and> seen between the HTML tags, and to begin with & # and HTML entities, so the display is not normal.

The solution is simple, it is rendered in the JS to the previous HTML page into what you can:

< Scripttype = "text / javascript">
var str = '< b> msg < / b> #';
document.all.div1.innerHTML = '< pre>' + str + '< / pre>';
// Js string normally displayed in an HTML page
String.prototype.displayHtml = function () {
// Will be converted into an array of strings
var strArr = this.split ( '');
// HTML page to display special characters, spaces, not the nature, but multiple spaces browser default show only one, so replace
var htmlChar = "& < >";
for (var i = 0; i < str.length; i ++) {
// Find contain special HTML characters
if (htmlChar.indexOf (str.charAt (i)) = -! 1) {
// If there is, then they are converted into the corresponding HTML entities
switch (str.charAt (i)) {
case '< ':
strArr.splice (i, 1, '<');
case '>':
strArr.splice (i, 1, '>');
case '&':
strArr.splice (i, 1, '&');
return strArr.join ( '');
alert (str.displayHtml ());
document.all.div2.innerHTML = str.displayHtml ();
< / Script>
escape () function
The function can handle spaces, slashes and anything else that may affect the contents of the browser, and convert them into Web available characters (eg, spaces will be converted to 20%, depending on which browser will not deal with space, but not change, it is passed directly to the server). Thereafter, the server (usually automatically) and then convert them back to their original transmission after the "face."

var url = "nowamagic.php people =?" + escape (people.toJSONString ());
request.open ( "GET", url, true);
request.onreadystatechange = updatePage;
request.send (null);
The disadvantage of this approach is twofold: when using the GET request to send large blocks of data, there is a string of URL length limit. Although this is very broad limits, but the length of the JSON string representation of the object may be beyond your imagination, especially when using an extremely complex subject even more so. In plain text across the network to send all the data, the transmission data is facing insecurity beyond your capabilities.

In short, these are two limitations GET request, rather than a simple JSON data associated with two things. When you want to send more than a user name and last name, such as the choice of the form, and they may need to pay more attention. To deal with any confidential or extremely long content, you can use the POST request.

Question marks
If you include in JSON or double quotes, it will destroy the JSON format. There are two ways to resolve.

In storage when you can use the addslashes () function handles what strings to the former quotes slash. Was changed characters include a single quote ( '), double quote ( "), backslash backslash () and the null character NULL.

$ Text = addslashes ($ text);
JavaScript, then it can be:

function valueReplace (v) {
v = v.toString () replace (newRegExp ( '([ "\"])', 'g'), "\\\" ").;
return v;
var eValue = encodeURI ($. trim (valueReplace (e.value)))
- Binary Tree Traversal (Linux)
- Java recognize simple codes (Programming)
- Cacti installation deployment under CentOS 6.6 (Server)
- Debian (Wheezy) Install Redmine 2.6 (Programming)
- Linux Tutorial Share: How to sudo command to define the PATH environment variable (Linux)
- Let Linux boot methods to enter characters interface and set FrameBuffer resolution methods (Linux)
- Linux shell string interception and stitching (Linux)
- Is Linux the most secure operating system (Linux)
- Linux Operating System Security Study (Linux)
- PHP with FastCGI and mod_php Comments (Server)
- RedHat Linux 7 build yum source server (Server)
- Linux NIC configuration (Linux)
- MySQL5.7 implement virtual column expression index (Database)
- Linux shell script to adjust the Task Scheduler (Linux)
- How to install Wine 1.7.20 under Ubuntu or Linux Mint (Linux)
- HomeKit Human Interface Guidelines (Linux)
- Java Timer (regular calling, to achieve a fixed time to perform) (Programming)
- Intruder tools Knark Analysis and Prevention Linux environment (Linux)
- Android webView URL redirects affect goBack () (Programming)
- xargs Detailed description (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.