关键词搜索

源码搜索 ×
×

Java 操作Fastjson JSON字符串转义正确处理方式

发布2020-08-11浏览4422次

详情内容

经常使用字符串替换replace或者replaceAll ,而replaceAll是基于正则表达式实现的。

本文重点关注的是批量替换replaceAll方法。

目录

替换源码

错误示例

正确示例

真实示例


替换源码

  1. /**
  2. * Replaces each substring of this string that matches the given <a
  3. * href="../util/regex/Pattern.html#sum">regular expression</a> with the
  4. * given replacement.
  5. *
  6. * <p> An invocation of this method of the form
  7. * <i>str</i>{@code .replaceAll(}<i>regex</i>{@code ,} <i>repl</i>{@code )}
  8. * yields exactly the same result as the expression
  9. *
  10. * <blockquote>
  11. * <code>
  12. * {@link java.util.regex.Pattern}.{@link
  13. * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
  14. * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link
  15. * java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>)
  16. * </code>
  17. * </blockquote>
  18. *
  19. *<p>
  20. * Note that backslashes ({@code \}) and dollar signs ({@code $}) in the
  21. * replacement string may cause the results to be different than if it were
  22. * being treated as a literal replacement string; see
  23. * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
  24. * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
  25. * meaning of these characters, if desired.
  26. *
  27. * @param regex
  28. * the regular expression to which this string is to be matched
  29. * @param replacement
  30. * the string to be substituted for each match
  31. *
  32. * @return The resulting {@code String}
  33. *
  34. * @throws PatternSyntaxException
  35. * if the regular expression's syntax is invalid
  36. *
  37. * @see java.util.regex.Pattern
  38. *
  39. * @since 1.4
  40. * @spec JSR-51
  41. */
  42. public String replaceAll(String regex, String replacement) {
  43. return Pattern.compile(regex).matcher(this).replaceAll(replacement);
  44. }
  45. /**
  46. * Replaces every subsequence of the input sequence that matches the
  47. * pattern with the given replacement string.
  48. *
  49. * <p> This method first resets this matcher. It then scans the input
  50. * sequence looking for matches of the pattern. Characters that are not
  51. * part of any match are appended directly to the result string; each match
  52. * is replaced in the result by the replacement string. The replacement
  53. * string may contain references to captured subsequences as in the {@link
  54. * #appendReplacement appendReplacement} method.
  55. *
  56. * <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
  57. * the replacement string may cause the results to be different than if it
  58. * were being treated as a literal replacement string. Dollar signs may be
  59. * treated as references to captured subsequences as described above, and
  60. * backslashes are used to escape literal characters in the replacement
  61. * string.
  62. *
  63. * <p> Given the regular expression <tt>a*b</tt>, the input
  64. * <tt>"aabfooaabfooabfoob"</tt>, and the replacement string
  65. * <tt>"-"</tt>, an invocation of this method on a matcher for that
  66. * expression would yield the string <tt>"-foo-foo-foo-"</tt>.
  67. *
  68. * <p> Invoking this method changes this matcher's state. If the matcher
  69. * is to be used in further matching operations then it should first be
  70. * reset. </p>
  71. *
  72. * @param replacement
  73. * The replacement string
  74. *
  75. * @return The string constructed by replacing each matching subsequence
  76. * by the replacement string, substituting captured subsequences
  77. * as needed
  78. */
  79. public String replaceAll(String replacement) {
  80. reset();
  81. boolean result = find();
  82. if (result) {
  83. StringBuffer sb = new StringBuffer();
  84. do {
  85. appendReplacement(sb, replacement);
  86. result = find();
  87. } while (result);
  88. appendTail(sb);
  89. return sb.toString();
  90. }
  91. return text.toString();
  92. }

错误示例

  1. // 回车符
  2. “string”.replaceAll("\r","");
  3. // 换行符
  4. “string”.replaceAll("\n","");
  5. // 缩进
  6. “string”.replaceAll("\t","");
  7. // 反斜杠
  8. “string”.replaceAll("\","");
  9. // 回车符
  10. “string”.replaceAll("\\r","");
  11. // 换行符
  12. “string”.replaceAll("\\n","");
  13. // 缩进
  14. “string”.replaceAll("\\t","");
  15. // 反斜杠
  16. “string”.replaceAll("\\","");

正确示例

  1. // 回车符
  2. “string”.replaceAll("\\\\r","");
  3. // 换行符
  4. “string”.replaceAll("\\\\n","");
  5. // 缩进
  6. “string”.replaceAll("\\\\t","");
  7. // 反斜杠
  8. “string”.replaceAll("\\\\","");

真实示例

  1. JSONObject jsonObject;
  2. for (String s : templateMap.keySet()) {
  3. String jsonStr = templateMap.get(s).toString();
  4. try{
  5. // 去掉换行、回车、缩进、转义字符
  6. jsonStr = jsonStr.replaceAll("\\\\n|\\\\r|\\\\t","");
  7. jsonObject = JSONObject.parseObject(jsonStr);
  8. String tableName = jsonObject.getString("TABLE_NAME");
  9. for (Map<String, Object> map : userMapList) {
  10. updateUserServiceData(tableName, map);
  11. }
  12. }catch (Exception e){
  13. log.error("JSON转换异常:{}",jsonStr);
  14. e.printStackTrace();
  15. }
  16. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载