Search code examples
javajsonsimplejson

URL issue with JSONObject


I've the following code, but when I am saving below JSON in database its giving me wrong url like {"#url#":"https:\/\/www.test.com\/test"}

import org.json.simple.JSONObject;

public class DemoURL {
    private static String url = "https://www.test.com/test";
    public static void main(String[] args) {
        JSONObject msgJson = new JSONObject();
        msgJson.put("#url#", url);
        System.out.println(msgJson.toString());
    }
}

I want url like {"#url#":"https://www.test.com/test"} Please suggest how to fix it?


Solution

  • Here is the solution:

    public class App{
        private static String url = "https://www.test.com/test";
        public static void main(String[] args) {
            JSONObject msgJson = new JSONObject();
            msgJson.put("#url#", url);
            System.out.println(getCleanURL(msgJson.toString()));
        }
    
        private static String getCleanURL(String url){
            return url.replaceAll("\\\\", "").trim();
        }
    }
    

    This gives correct output, simply run this code. This will store exact value in the database.

    {"#url#":"https://www.test.com/test"}