我有以下代码,但是当我在数据库中保存低于JSON时,它给了我错误的网址,例如{"#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"}
这样的网址
请建议如何解决?
答案 0 :(得分:2)
以下是解决方案:
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();
}
}
这样可以提供正确的输出,只需运行此代码即可。这将在数据库中存储确切的值。
{"#url#":"https://www.test.com/test"}
答案 1 :(得分:1)
您正在使用org.json.simple
JSON库。 JSON-Simple从String中转义char。
你无法改变这一点,因为它不可配置。
但你可以使用org.json
JSON库,这不会逃避String,而且很重要的是,你不必更改你的代码,现有的语法也能正常工作。
e.g。
import org.json.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"}
答案 2 :(得分:1)
尝试将斜杠(/)替换为unicode字符\ u2215,然后再将其传递给JSON对象。
答案 3 :(得分:-1)
尝试用单引号引用字符串,类似这样
protected void sendJson(final String email, final String pwd) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser");
Query queryObj = new Query();
queryObj.setLogin("WT");
queryObj.setPassword("3");
json.put("Query", queryObj);
// json.put("email", email);
// json.put("password", pwd);
json.put("includeUserMiscInfo", true);
StringEntity se = new StringEntity( json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
Toast.makeText(getActivity().getApplicationContext(), "Response:" + convertStreamToString(in),Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
e.printStackTrace();
// getActivity().createDialog("Error", "Cannot Estabilish Connection");
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}