{"id":1517,"date":"2015-05-26T16:15:56","date_gmt":"2015-05-26T08:15:56","guid":{"rendered":"https:\/\/cowmanchiang.me\/wp\/?p=1517"},"modified":"2023-10-31T15:43:30","modified_gmt":"2023-10-31T07:43:30","slug":"java-json-escape-special-character","status":"publish","type":"post","link":"https:\/\/cowmanchiang.me\/wp\/?p=1517","title":{"rendered":"[Java] JSON escape special character"},"content":{"rendered":"<p>Ref: <a href=\"http:\/\/blog.sina.com.cn\/s\/blog_5dc034a30100jnpp.html\">\u5251\u98d8\u6c5f\u6e56\u7684\u535a\u5ba2 &#8211; JSON\u683c\u5f0f\u6570\u636e\u4e2d\u7279\u6b8a\u5b57\u7b26\u7684\u5904\u7406<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">   public static String stringToJson(String s) {\n    if (StringUtils.isEmpty(s)) {\n        return s;\n    }\n    StringBuffer sb = new StringBuffer ();\n    for (int i=0; i&lt;s.length(); i++) {\n\n        char c = s.charAt(i);\n        switch (c) {\n            case &#039;&quot;&#039;:\n                sb.append(&quot;\\&quot;&quot;);\n                break;\n            \/*case &#039;\\&#039;:   \n                sb.append(&quot;\\\\&quot;);\n                break;\n            case &#039;\/&#039;:\n                sb.append(&quot;\\\/&quot;);\n                break;*\/\n            case &#039;b&#039;:      \n                sb.append(&quot;\\b&quot;);\n                break;\n            case &#039;f&#039;:      \n                sb.append(&quot;\\f&quot;);\n                break;\n            case &#039;n&#039;:\n                sb.append(&quot;\\n&quot;); \n                break;\n            case &#039;r&#039;:      \n                sb.append(&quot;\\r&quot;);\n                break;\n            case &#039;t&#039;:      \n                sb.append(&quot;\\t&quot;);\n                break;\n            default:\n                sb.append(c);\n        }\n    }\n    return sb.toString();\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ref: \u5251\u98d8\u6c5f\u6e56\u7684\u535a\u5ba2 &#8211; JSON\u683c\u5f0f\u6570\u636e\u4e2d\u7279\u6b8a\u5b57\u7b26\u7684\u5904\u7406 public static String stringToJson(String s) { if (StringUtils.isEmpty(s)) { return s; } StringBuffer sb = new StringBuffer (); for (int i=0; i&lt;s.length(); i++) { char c = s.charAt(i); switch (c) { case &#039;&quot;&#039;: sb.append(&quot;\\&quot;&quot;); break; \/*case &hellip; <a href=\"https:\/\/cowmanchiang.me\/wp\/?p=1517\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-1517","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1517"}],"version-history":[{"count":1,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1517\/revisions"}],"predecessor-version":[{"id":1909,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1517\/revisions\/1909"}],"wp:attachment":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}