Escapes json characters in the passed string - Java java.lang

Java examples for java.lang:String Escape

Description

Escapes json characters in the passed string

Demo Code

//package com.java2s;

public class Main {


    /**/*  w ww .  j  av  a 2s .c  o m*/
     * Escapes json characters in the passed string
     * @param s The string to esacape
     * @return the escaped string
     */
    public static String jsonEscape(CharSequence s) {
        return s.toString().replace("\"", "\\\"").replace("[", "\\[")
                .replace("]", "\\]").replace("{", "\\{")
                .replace("}", "\\}");
    }
}

Related Tutorials