首页 > 解决方案 > 从java中的JSONObject中删除特殊字符

问题描述

我正在通过返回 xml 的 java 调用 API。

我的代码是:

JSONObject responses_obj = new JSONObject();
string url = "http://localhost:8080/myAPI";
URL obj = new URL(null, url , new sun.net.www.protocol.http.Handler());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

String urlParameters = "{\"patient_id\":" +"\"" +document_id_list[2] +"\", \"dob\" : " + "\"" + date_of_birth +  "\", \"document_id\" : " + "\""+ document_id + "\"}";

con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text");

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(
                                new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer responseJSON = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    responseJSON.append(inputLine);
    System.out.println(responseJSON);

    responses_obj.put("getDocument",responseJSON);
}
in.close()

当我打印 responseJSON 时,我可以看到它。但是,当我在浏览器中查看response_obj 的内容时,我可以看到如下内容:<ClinicalDocument xmlns=\"urn:hl7-org:v3\" xmlns:epsos=\"....

正确的应该是<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:epsos="...

有没有办法删除这些“\”字符?

标签: javaxml

解决方案


推荐阅读