Home  >  Article  >  Web Front-end  >  What are the escape characters of json

What are the escape characters of json

(*-*)浩
(*-*)浩Original
2019-06-01 13:32:399395browse

Escape Sequence is also called Character Entity. In HTML, there are two reasons for defining escaped strings: The first reason is that symbols like "<" and ">" have been used to represent HTML tags, so they cannot be directly used as text in the text. symbols to use. In order to use these symbols in HTML documents, you need to define their escape strings. When the interpreter encounters such a string, it interprets it as a real character. When entering escape strings, strictly follow the rules for upper and lower case letters. The second reason is that some characters are not defined in the ASCII character set, so they need to be represented by escaped strings.

What are the escape characters of json

In fact, all programming languages ​​have escape characters for basically two reasons:

1. Use escape characters To represent characters defined in the character set, such as control characters and carriage return and line feed characters in ASCll, these characters do not have ready-made text codes. So it can only be represented by escape characters.

2. Some specific characters are defined as special-purpose characters in the editing language.

Because these characters are defined for special purposes, they have lost their original meaning. For example, in Html, < is defined by HTML as the beginning of a tag, so when we switch to <, HTML will treat it as the beginning instead of a <. Another example is PHP's double quotes ("), which are defined by PHP as the peripheral tags of strings, so if you are inside a pair of double quotes and want to use double quotes, you can only use escape characters. Otherwise, PHP will report an error .

It can also be seen from the above that escaping is nothing more than two situations:

1: Convert ordinary characters to special purposes, usually in programming languages, Used to represent characters that cannot be displayed directly, such as the back key, enter key, etc.

2: Used to convert special meaning characters back to their original meaning. Generally used in regular expressions. Also Some scripting languages ​​are weakly typed. Some languages, such as HTML, are not programming languages, but markup languages. Some languages ​​have only one type, such as shell scripting languages. In these languages, the strings are not quoted "", or can be left without quotes. " ", so sometimes it is necessary to escape characters to indicate that the identity of a certain character at this time is a normal character, rather than a metacharacter with special meaning.

In addition, for the security of the website. Before the data is written to the database, Escape characters (functions) will be used to escape some sensitive characters. This can avoid injection attacks by people with ulterior motives who use special symbols.

The JSon string is saved in the file after being serialized. When reading a string, it cannot be directly parsed into a JSON object using JSON.parse(). Because it is a string, not a legal JSON object format.

For example, the following The JSON string is saved in the file and cannot be parsed directly when read:

“{\"resourceId\":\"dfead70e4ec5c11e43514000ced0cdcaf\",\"properties\":{\"process_id\":\"process4\",\"name\":\"\",\"documentation\":\"\",\"processformtemplate\":\"\"}}"

If this string appears in the code, there is no problem, because the backslash means escape, but if it comes from If the text file is read directly like this, it cannot be parsed directly with JSON.parse(). It needs to be deserialized and the backslashes are eliminated. Some programmers like to write a tool class to do such a thing, but it has been There are convenient and fast tools that can do this without us having to reinvent the wheel.

You can directly use Apache’s common.lang toolkit (use tool class: org.apache.commons.lang. StringEscapeUtils).

The example is as follows:

public void test(){
	String str1 = "{\"resourceId\":\"dfead70e4ec5c11e43514000ced0cdcaf\",\"properties\":{\"process_id\":\"process4
	"+"\",\"name\":\"\",\"documentation\":\"\",\"processformtemplate\":\"\"}}";
	String tmp = StringEscapeUtils.unescapeJavaScript(str1);
	System.out.println("tmp:" + tmp);
}

Output:

tmp:{"resourceId":"51ad70e41c5c11e88514000c290cdcfa","properties":{"process_id":"process4","name":"","documentation":"","processformtemplate":""}}

The above is the detailed content of What are the escape characters of json. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:What is WeChat jsNext article:What is WeChat js