json special character handling

Use URLEncoder.encode(value, "utf-8") to encode and then use

URLDecoder.decode
decode If it contains the character "%", an exception will be thrown java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern -,
the '%' in the above string is a Chinese character 'yes', and the implementation of the conversion It converts the two characters after the % into a hexadecimal number. Take "% is" to convert the number, there will definitely be a NumberFormatException exception.

Similarly, if there is a '+' in the request string, there will be a problem. Because '+' is used as a space. One solution is to replace % with %25.
data = data.replaceAll("%(?![0-9a-fA-F]{2})", "%25"); 
data = data.replaceAll("\\+", "%2B"); 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611876&siteId=291194637