The original Java Escape Character KanKan, jsonkankan

Source: Internet
Author: User
Tags control characters

The original Java Escape Character KanKan, jsonkankan

Kankan original

This format is required when sending data with the php background.


Private void sendJson (){

// Initialize the Custom handler

CashHandler handler = new CashHandler (this );

// Request the primary address, which is written in the Custom Application, followed by the interface Name
String url = MApplication. get (). getAppServiceUrl () + "order/submit ";

// Custom sending Request Method
LReqEntity entity = new LReqEntity ();
Entity. setUrl (url );
// In Lreqentity, THE post method. The parameter format is Map (String, String)

Map <String, String> map = new HashMap <String, String> ();

// Concatenate a string

StringBuffer params = new StringBuffer ();
// If JUMPKEY is 7 submitted from the credit Mall, it is bought from the credit mall

If (JUMPKEY. equals ("7 ")){

// Dbmanager is a user-defined database query method that queries the final order purchased by the shopping cart.

List <CarteEntity> cartList = DBManager. get (). queryFinalCarte (

UserEntity. id );

// Concatenate a string

Params. append ("[");
For (int I = 0; I <cartList. size (); I ++ ){
CarteEntity scoreEntity = new CarteEntity ();

ScoreEntity = cartList. get (I );

// The next line is an escape character. To send the character, concatenate "this character, Escape Character \"

// The sending result is {"A": 54}

Params. append ("{\" \":");
Params. append (scoreEntity. id + "");
Params. append (",");
Params. append ("\" B \":");
Params. append (scoreEntity. buyCount );
Params. append (",");
Params. append ("\" C \":");
Params. append ("\" score \"}");
If (I <cartList. size ()-1 ){
Params. append (",");
}
}


Params. append ("]");

}

Entity. setParams (map );
Entity. setReqMode (ReqMothed. POST );


Handler. startLoadingData (entity );


}


In this way, the splicing requirement is met, http: // 192.168.100.180: 85/service/order/submit? {A = [{"A": 95, "B": 3, "C": "score" },{ "A": 99, "B": 3, "C": "score"}], B = 89}


There are many escape characters,


JAVA escape characters:

The first one is the most.

1. special characters: Three
\ ": Double quotation marks
\ ': Single quotes
\: Backslash




2. octal escape sequence: \ + 1 to 3 digits and 5 digits; range: '\ 000 '~ '\ 100'
\ 0: NULL characters
3. Unicode escape characters: \ u + four hexadecimal numbers; 0 ~ 65535
\ U0000: NULL Character

4. control characters: 5

\ 'Single quotes

\ Backslash character

\ R press ENTER

\ N line feed

\ F go to paper form

\ T horizontal hop

\ B Return

Point escape:. ==> u002E
Escape of dollar signs: $ ==> u0024
Escape of the multiplication Symbol: ^ ==> u005E
Escape from the left braces: {==> u007B
Escape from left square brackets: [==> u005B
Escape of left parentheses :( ==> u0028
Escape of vertical bars: |=> u007C
Escape of right parentheses: ==> u0029
Asterisk escape: * ==> u002A
Escape of the plus sign: + ==> u002B
Escape question mark :? ==> U003F
Escape the backslash: ==> u005C
========================================================== ====================================
The following program uses two Unicode escape characters, which use their hexadecimal code to represent Unicode characters. So what will this program print?
Java code
Public class EscapeRout {
Public static void main (String [] args ){
// \ U0022 is the Unicode Escape Character of double quotation marks
System. out. println ("a \ u0022.length ()
+ \ U0022b ". length ());
}
}

Public class EscapeRout {
Public static void main (String [] args ){
// \ U0022 is the Unicode Escape Character of double quotation marks
System. out. println ("a \ u0022.length ()
+ \ U0022b ". length ());
}
}


A superficial analysis of the program will think that it should print out 26, because in the two double quotes "a \ u0022.length () + \ u0022b "indicates a string of a total of 26 characters.
A little deeper analysis will assume that the program should print 16, because each of the two Unicode escape characters must be represented by six characters in the source file, but they only represent one character in the string. Therefore, this string should be 10 characters shorter than its appearance. If you run this program, you will find that this is far from the case. It neither prints 26 nor 16, but 2.

The key to understanding this puzzle is to know that Java does not provide any special processing for Unicode escape characters in string literal constants. Before parsing a program into symbols, the compiler converts Unicode escape characters into the characters they represent [JLS 3.2]. Therefore, the first Unicode Escape Character in the program is used as the ending quotation mark of a single character string literal constant (", the second Unicode escape character is used as the start quotation mark of another single character string literal constant ("B. The program prints the expression "a". length () + "B". length (), that is, 2.

If the author of the program really wants this behavior, the following statement will be much clearer:


Java code
System. out. println ("a". length () + "B". length ());

It is more likely that the author wants to place two double quotation marks inside the string literal constant. You cannot use Unicode escape characters, but you can use the Escape Character Sequence to implement [JLS 3.10.6]. The Escape Character Sequence of a double quotation mark is a backslash followed by a double quotation mark (\"). If you replace the Unicode Escape Character in the original program with the Escape Character Sequence, it prints the expected 16 (error, it should be 14, do not know how it will come out 16):

Java code
System. out. println ("a \". length () + \ "B". length ());

Many characters have corresponding escape character sequences, including single quotes (\ '), line breaks (\ n), tabs (\ t), and backslash (\\). You can use escape character sequences in character literal constants and string literal constants.
In fact, you can place any ASCII character in a string literal constant or a character literal constant by using a special series of escape characters called octal escape characters, however, it is best to use common escape character sequences as much as possible.

The common escape character sequences and octal escape characters are much better than the Unicode escape characters, because different from the Unicode escape characters, the escape character sequences are processed after the program is parsed into various symbols.

ASCII is the minimum common feature set of a character set. It contains only 128 characters, But Unicode contains more than 65,000 characters. A Unicode escape character can be used to insert a Unicode character in a program that only uses ASCII characters. A Unicode escape character is exactly equivalent to the character it represents.

Unicode escape characters are designed to be used when a programmer needs to insert a character that cannot be expressed in the source file character set. They are mainly used to place non-ASCII characters in identifiers, string literal constants, character literal constants, and comments. Occasionally, Unicode escape characters are used to clearly identify one of several seemingly similar characters to increase program clarity.

In short, the escape character sequence should be preferred in character strings and literal constants, rather than Unicode escape characters. Unicode escape characters may be confusing because they are processed too early in the compilation sequence. Do not use Unicode escape characters to represent ASCII characters. In character strings and character literal constants, escape character sequences should be used; in addition to these literal constants, ASCII characters should be directly inserted into the source file.

 

 

 


Meaning of escape sequence
\ N press enter (\ u000a)
\ T horizontal tab (\ u0009)
\ B space (\ u0008)
\ R line feed (\ u000d)
\ F form feed (\ u000c)
\ 'Single quotes (\ u0027)
\ "Double quotation marks (\ u0022)
\ Backslash (\ u005c)
\ Ddd three-digit gossip
\ Udddd four-digit hexadecimal
JAVA escape characters:
1. octal escape sequence:
\ + 1 to 3 5 digits; range: '\ 000 '~ '\ 100'
\ 0: NULL characters
2. Unicode escape characters:
\ U + four hexadecimal numbers; 0 ~ 65535
\ U0000: NULL Character
The octal value is a number ranging from 1 to 3, and the value range must be greater than the value range. If the value range is exceeded, a compilation error occurs. The hexadecimal escape must be four hexadecimal digits, in addition, some cannot be escaped, such as '\ u000a', cannot be transferred, compilation errors may occur, and a considerable number does not have the corresponding characters, it is converted to a question mark and escaped from a number, as long as it does not exceed the range.
3. Special characters: three \ ": Double quotation marks \ ': single quotation marks \: backslash
4. control characters: 5 \ 'single quotation mark character \ backslash character \ r press enter \ n line feed \ f take paper for another page \ t horizontal jump \ B Return
Point escape:. ==> u002E
Escape of dollar signs: $ ==> u0024
Escape of the multiplication Symbol: ^ ==> u005E
Escape from the left braces: {==> u007B
Escape from left square brackets: [==> u005B
Escape of left parentheses :( ==> u0028
Escape of vertical bars: |=> u007C
Escape of right parentheses: ==> u0029
Asterisk escape: * ==> u002A
Escape of the plus sign: + ==> u002B
Escape question mark :? ==> U003F
Escape the backslash: ==> u005C



Java: The JSON string has been escaped in Java. How can I concatenate a function?

Do you want this result?
{"Id": "No data", "name": "No data", "a": "No data", "time": "getData ()"}

JQuery ajax returns the json format, and the browser displays escape characters

This is generally caused by incomplete JSON. You can process the data in this way.
$. Post (url, data, function (youjson) {youjson = $. parseJSON (youjson); // you can continue to process your code });

Related Article

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.