JSONObject的URL问题 [英] URL issue with JSONObject

查看:137
本文介绍了JSONObject的URL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但是当我在数据库中保存低于JSON时,它给了我错误的网址,例如 {#url#:https:\ / \ / www.test .com\ / test}

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());
    }
}

我想要网址,如 { #url#:https://www.test.com/test}
请建议如何修复它?

推荐答案

以下是解决方案:

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"} 

这篇关于JSONObject的URL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆