Here you can find the source of jsonString(String s)
public static String jsonString(String s)
//package com.java2s; //License from project: Open Source License import java.text.CharacterIterator; import java.text.StringCharacterIterator; public class Main { static char[] hex = "0123456789ABCDEF".toCharArray(); public static String jsonString(String s) { if (s == null) return null; StringBuffer b = new StringBuffer(); b.append("\""); CharacterIterator it = new StringCharacterIterator(s); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { if (c == '"') b.append("\\\""); else if (c == '\\') b.append("\\\\"); else if (c == '/') b.append("\\/"); else if (c == '\b') b.append("\\b"); else if (c == '\f') b.append("\\f"); else if (c == '\n') b.append("\\n"); else if (c == '\r') b.append("\\r"); else if (c == '\t') b.append("\\t"); else if (Character.isISOControl(c)) { int n = c; for (int i = 0; i < 4; ++i) { int digit = (n & 0xf000) >> 12; b.append(hex[digit]); n <<= 4;/*from w w w . jav a 2 s . c o m*/ } } else { b.append(c); } } b.append("\""); return b.toString(); } }