如何在java中转义特殊字符restfulapi查询字符串

iqxoj9l9  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(576)

我尝试修改现有查询字符串以筛选指定的条件:现在查询字符串如下:

  1. String bodycontent="{"
  2. +" \"_source\": [\"@timestamp\",\"eqpid\",\"lotid\",\"stageid\",\"srvmethod\",\"parm_1\",\"apcack\",\"description\",\"host\",\"path\",\"action\"],"
  3. +" \"query\": {"
  4. +" \"query_string\" : {"
  5. +" \"query\":\" lotid:"+q_lotid+" AND "+host_type+"\"}"
  6. +" },"
  7. +" \"sort\" : [{\"@timestamp\" : { \"order\" : \"desc\" }}]"
  8. +"}";

我想更改以下查询条件:

  1. query :
  2. type:ams_log AND (alm_source:K*) AND (alm_id:TCS00004 OR alm_id:TCS00005 OR alm_id:TCS00007 OR alm_id:TCS00008 OR alm_id:TCS00009 OR alm_id:TCS00010 OR alm_id:TCS00011 OR alm_id:TCS00012 OR alm_id:TCS00013 OR alm_id:TCS00020 OR alm_id:TCS00024 OR alm_id:TCS00032)

但是我不知道如何用斜杠修改查询字符串 \ 和双引号 " . 我找不到规则,谢谢!

qcuzuvrc

qcuzuvrc1#

斜杠- \\ 引用- \" ```
System.out.println("\ "hello"");

  1. 输出: `\ "hello"` 关于您的查询,似乎keyvalue都必须在引号之间 `"key":"value"` ```
  2. String lotId="2020_A_88";
  3. String type="apples";
  4. String queryPart = "\"lotId\":\""+lotId+"\" AND \"type\":\""+type+"\"";
  5. System.out.println(queryPart);
  6. ``` `"lotId":"2020_A_88" AND "type":"apples"` ```
  7. String example = String.format("Slash-> %s , Quotes-> %s", "\\" ,"\"name\"");
  8. System.out.println(example);
  9. ``` `Slash-> \ , Quotes-> "name"` ```
  10. String s = "\\";
  11. String q = "\"name\"";
  12. System.out.println("Slash-> "+s+" , Quotes-> "+q);
  13. ``` `Slash-> \ , Quotes-> "name"`

相关问题