private static String escapeJSON(String text){
  StringBuilder builder=new StringBuilder();
  builder.append('"');
  for (int index=0; index < text.length(); index++) {
    char chr=text.charAt(index);
switch (chr) {
case '"':
case '\\':
      builder.append('\\');
    builder.append(chr);
  break;
case '\b':
builder.append("\\b");
break;
case '\t':
builder.append("\\t");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
default :
if (chr < ' ') {
String t="000" + Integer.toHexString(chr);
builder.append("\\u" + t.substring(t.length() - 4));
}
 else {
builder.append(chr);
}
break;
}
}
builder.append('"');
return builder.toString();
}

private static String escapeJSON ( String text ) { StringBuilder builder = new StringBuilder ( ) ; builder . append ( '"' ) ; for ( int index = 0 ; index < text . length ( ) ; index ++ ) { char chr = text . charAt ( index ) ; switch ( chr ) { case '"' : case '\\' : builder . append ( '\\' ) ; builder . append ( chr ) ; break ; case '\b' : builder . append ( "\\b" ) ; break ; case '\t' : builder . append ( "\\t" ) ; break ; case '\n' : builder . append ( "\\n" ) ; break ; case '\r' : builder . append ( "\\r" ) ; break ; default : if ( chr < ' ' ) { String t = "000" + Integer . toHexString ( chr ) ; builder . append ( "\\u" + t . substring ( t . length ( ) - 4 ) ) ; } else { builder . append ( chr ) ; } break ; } } builder . append ( '"' ) ; return builder . toString ( ) ; }
escape a string to create a valid json string
