English 中文(简体)
Escaping Special Characters
  • 时间:2024-12-22

JSON.simple - Escaping Special Characters


Previous Page Next Page  

The following characters are reserved characters and can not be used in JSON and must be properly escaped to be used in strings.

    Backspace to be replaced with 

    Form feed to be replaced with f

    Newpne to be replaced with

    Carriage return to be replaced with

    Tab to be replaced with

    Double quote to be replaced with "

    Backslash to be replaced with \

JSONObject.escape() method can be used to escape such reserved keywords in a JSON String. Following is the example −

Example

import org.json.simple.JSONObject;

pubpc class JsonDemo {
   pubpc static void main(String[] args) {
      JSONObject jsonObject = new JSONObject();
      String text = "Text with special character /" f	
.";
      System.out.println(text);
      System.out.println("After escaping.");
      text = jsonObject.escape(text);
      System.out.println(text);
   }
}

Output

Text with special character /" 
.
After escaping.
Text with special character /" f	
.
Advertisements