Escape Character Utility สำหรับข้อมูล URL และ JSON – ใช้งานใน Java Project ได้ตามสบาย
เผยแพร่แล้ว: 2021-11-19
อักขระหลีกใน Java คืออะไร?
อักขระหลีกส่วนใหญ่คืออักขระ which replaces existing character
ด้วยอักขระ new & provided character
ซึ่งทำงานได้ดีที่สุดโดยไม่เกิดข้อผิดพลาดขณะใช้งานจริง
นอกจากนี้ยังจำเป็นสำหรับการส่งระหว่างระบบ/กระบวนการ เช่น char เดียวกันใช้งานได้กับ C++, Java, ภาษาการเขียนโปรแกรม ฯลฯ
ในบทช่วยสอนนี้ เราจะพูดถึง Escape Character Utilities สองแบบ
- URLEscapeUtil
- JSONEscapeUtil
บทช่วยสอนนี้จะช่วยคุณหากคุณมีคำถามด้านล่าง:
- อักขระหลีก Java ในสตริง
- อักขระใดที่ต้องหลีกเลี่ยงใน HTML
- Escape char หมายถึงอะไรในภาษา java
- อักขระหลีก Java ในสตริง
- ฉันจะหลีกเลี่ยงสตริงสำหรับ Java ได้อย่างไร
- Escape Sequences ใน Java พร้อมตัวอย่าง
มาเริ่มกันเลย:
- สร้างคลาส CrunchifyEscapeCharUtility.java
- สร้างเมธอด crunchifyURLEscapeUtil(String s) – ซึ่งส่งคืนสตริงด้วยอักขระหลีกใน URL ที่ให้มา
- สร้างเมธอด crunchifyJSONEscapeUtil(String s) – ซึ่งส่งคืน String พร้อมอักขระหลีกใน JSON . ที่ให้มา
- ในหลัก () –
- เราจะอ่านข้อมูล JSON จากไฟล์
- ระบุชื่อไฟล์คือ
Crunchify_Escape_Util.txt
- เราใช้
java.net.URLEncoder
เพื่อเพิ่ม URL Escape char
ใน Java เมื่อเราเข้ารหัสสตริง จะใช้กฎต่อไปนี้:

นี่คือเนื้อหาไฟล์ Crunchify_Escape_Util.txt
โปรดใส่ลงในแล็ปท็อป/เดสก์ท็อปและอัปเดตตำแหน่งเส้นทางในโปรแกรม
1 2 3 4 5 6 7 8 9 10 |
{ "founder" : "App Shah" , "blogURL" : "https://crunchify.com" , "twitter" : "https://twitter.com/Crunchify" , "social" : { "facebook" : "http://facebook.com/Crunchify" , "pinterest" : "https://www.pinterest.com/Crunchify/crunchify-articles”, " rss ": " https : //crunchify.com/feed/" } } |
ตัวเลือกที่ 1: นี่คือตัวอย่าง Java ที่สมบูรณ์
- สร้าง Escape JSON และเมธอด URL ของเราเอง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
package crunchify . com . tutorials ; import java . io . BufferedReader ; import java . io . FileReader ; import java . io . IOException ; import java . net . URLEncoder ; import java . nio . charset . StandardCharsets ; import java . text . StringCharacterIterator ; /** * @author Crunchify.com * Version: 1.0 * Program: Escape Character Utility for URL and JSON data - Feel free to use in your Java Project. */ public class CrunchifyEscapeCharUtility { public static void main ( String [ ] args ) { // URL Escape Utility String crunchifyURL = "https://crunchify.com/this is test" ; crunchifyLog ( "Sample URL: " + crunchifyURL ) ; crunchifyLog ( "Escaped URL: " + crunchifyURLEscapeUtil ( crunchifyURL ) ) ; // JSON Escape Utility // We will read file first and then we will do escape char on that StringBuilder jsonData = new StringBuilder ( ) ; BufferedReader crunchifyBufferReader = null ; try { String crunchifyLine ; crunchifyBufferReader = new BufferedReader ( new FileReader ( "/Users/app/Downloads/Crunchify_Escape_Util.txt" ) ) ; while ( ( crunchifyLine = crunchifyBufferReader . readLine ( ) ) ! = null ) { jsonData . append ( crunchifyLine ) . append ( "\n" ) ; } // IOException: Signals that an I/O exception of some sort has occurred. // This class is the general class of exceptions produced by failed or interrupted I/O operations. } catch ( IOException exception ) { exception . printStackTrace ( ) ; } finally { try { if ( crunchifyBufferReader ! = null ) crunchifyBufferReader . close ( ) ; } catch ( IOException ex ) { ex . printStackTrace ( ) ; } } // Let's print raw JSON file data crunchifyLog ( "\n Sample JSON: " + jsonData . toString ( ) . toString ( ) ) ; // Let's print data after escaping JSON strings crunchifyLog ( "Escaped JSON: " + crunchifyJSONEscapeUtil ( jsonData . toString ( ) . toString ( ) ) ) ; } // Used to ensure that HTTP query strings are in proper form, by escaping special characters such as spaces. // Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme public static String crunchifyURLEscapeUtil ( String crunchifyURL ) { String crunchifyNewURL ; // UTF_8: Eight-bit UCS Transformation Format. crunchifyNewURL = URLEncoder . encode ( crunchifyURL , StandardCharsets . UTF_8 ) ; return crunchifyNewURL ; } // JSON Escape Utility public static String crunchifyJSONEscapeUtil ( String crunchifyJSON ) { // StringBuilder(): Constructs a string builder with no characters in it and an initial capacity of 16 characters. final StringBuilder crunchifyNewJSON = new StringBuilder ( ) ; // StringCharacterIterator class iterates over the entire String StringCharacterIterator crunchifyIterator = new StringCharacterIterator ( crunchifyJSON ) ; // current(): Implements CharacterIterator.current() for String. char crunchifyChar = crunchifyIterator . current ( ) ; // DONE = \\uffff (not a character) while ( crunchifyChar ! = StringCharacterIterator . DONE ) { if ( crunchifyChar == '\"' ) { crunchifyNewJSON . append ( "\\\" "); } else if (crunchifyChar == '\t') { crunchifyNewJSON.append(" \ \ t "); } else if (crunchifyChar == '\f') { crunchifyNewJSON.append(" \ \ f "); } else if (crunchifyChar == '\n') { crunchifyNewJSON.append(" \ \ n "); } else if (crunchifyChar == '\r') { crunchifyNewJSON.append(" \ \ r "); } else if (crunchifyChar == '\\') { crunchifyNewJSON.append(" \ \ \ \ "); } else if (crunchifyChar == '/') { crunchifyNewJSON.append(" \ \ / "); } else if (crunchifyChar == '\b') { crunchifyNewJSON.append(" \ \ b " ) ; } else { // Nothing matched - just as text as it is. // next(): Implements CharacterIterator.next() for String. crunchifyNewJSON . append ( crunchifyChar ) ; } crunchifyChar = crunchifyIterator . next ( ) ; } return crunchifyNewJSON . toString ( ) ; } // Simple log utility private static void crunchifyLog ( String crunchifyData ) { System . out . println ( crunchifyData ) ; } } |
เอาต์พุตคอนโซล Eclipse:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Sample URL : https : //crunchify.com/this is test Escaped URL : https % 3A % 2F % 2Fcrunchify.com % 2Fthis + is + test Sample JSON : { "founder" : "App Shah" , "blogURL" : "https://crunchify.com" , "twitter" : "https://twitter.com/Crunchify" , "social" : { "facebook" : "http://facebook.com/Crunchify" , "pinterest" : "https://www.pinterest.com/Crunchify/crunchify-articles”, " rss ": " https : //crunchify.com/feed/" } } Escaped JSON : { \ n \ " founder \ " : \ " App Shah \ " , \ n \ " blogURL \ " : \ " https : \ / \ / crunchify . com \ " , \ n \ " twitter \ " : \ " https : \ / \ / twitter . com \ / Crunchify \ " , \ n \ " social \ " : { \ n \ " facebook \ " : \ " http : \ / \ / facebook . com \ / Crunchify \ " , \ n \ " pinterest \ " : \ " https : \ / \ / www . pinterest . com \ / Crunchify \ / crunchify - articles ” , \ n \ " rss \ " : \ " https : \ / \ / crunchify . com \ / feed \ / \ " \ n } \ n } \ n Process finished with exit code 0 |
ตัวเลือก-2: การใช้การพึ่งพาข้อความทั่วไปของ Apache Common
- เพิ่มการพึ่งพา pom.xml ของ Maven
1 2 3 4 5 |
< dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - text < / artifactId > < version > 1.9 < / version > < / dependency > |
รหัสที่สมบูรณ์:
- สร้างคลาส: CrunchifyEscapeJSONTutorial.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package crunchify . com . java . tutorials ; import org . apache . commons . text . StringEscapeUtils ; /** * @author Crunchify.com * Program: In Java How to Escape JSON? Simple Tutorial using Apache Commons Library. */ public class CrunchifyEscapeJSONTutorial { public static void main ( String [ ] args ) { // StringEscapeUtils: Escapes and unescapes Strings for Java, Java Script, HTML and XML. // #ThreadSafe# // This code has been adapted from Apache Commons Lang 3.5. String crunchifyHTML = "{\n" + " \"founder\": \"App Shah\",\n" + " \"blogURL\": \"https://crunchify.com\",\n" + " \"twitter\": \"https://twitter.com/Crunchify\",\n" + " \"social\": {\n" + " \"facebook\": \"http://facebook.com/Crunchify\",\n" + " \"pinterest\": \"https://www.pinterest.com/Crunchify/crunchify-articles”,\n" + " \"rss\": \"https://crunchify.com/feed/\"\n" + " }\n" + "}" ; // escapeJson: Escapes the characters in a String using Json String rules. // Escapes any values it finds into their Json String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) // So a tab becomes the characters '\\' and 't'. // The only difference between Java strings and Json strings is that in Json, forward-slash (/) is escaped. String crunchifyResult = StringEscapeUtils . escapeJson ( crunchifyHTML ) ; crunchifyPrintUtils ( crunchifyResult ) ; } private static void crunchifyPrintUtils ( String crunchifyResult ) { System . out . println ( crunchifyResult ) ; } } |
ผลลัพธ์ของ IntelliJ IDEA:
เราย้ายไปที่ IntelliJ IDEA ตั้งแต่สองปีที่ผ่านมา นี่คือผลลัพธ์ของคอนโซล

1 2 3 |
{ \ n \ " founder \ " : \ " App Shah \ " , \ n \ " blogURL \ " : \ " https : \ / \ / crunchify . com \ " , \ n \ " twitter \ " : \ " https : \ / \ / twitter . com \ / Crunchify \ " , \ n \ " social \ " : { \ n \ " facebook \ " : \ " http : \ / \ / facebook . com \ / Crunchify \ " , \ n \ " pinterest \ " : \ " https : \ / \ / www . pinterest . com \ / Crunchify \ / crunchify - articles \ u201D , \ n \ " rss \ " : \ " https : \ / \ / crunchify . com \ / feed \ / \ " \ n } \ n } Process finished with exit code 0 |
แจ้งให้เราทราบหากคุณประสบปัญหาในการใช้งาน Java Application นี้