[英]How to avoid quotes around string values while creating JSONObject
I am trying to create JSON query string for MongoDB, In order to support regular expression I need to create JSON string that has values without quotes. 我正在尝试为MongoDB创建JSON查询字符串,为了支持正则表达式,我需要创建具有不带引号的值的JSON字符串。 How do accomplish this in java, 如何在Java中做到这一点,
Here is the example of what I am trying to do, 这是我要做的事的例子,
JSONObject obj= new JSONObject();
String title = "gro";
obj.put("title", "/.*" + title + ".*/");
Result is 结果是
{"title ":"/.*gro.*/"}
Above is no longer treated as a regular expression by MongoDB. MongoDB不再将上述内容视为正则表达式。 What i wanted is, 我想要的是
{"title ":/.*gro.*/}
So that MongoDB treats it as a regular expression. 这样MongoDB会将其视为正则表达式。 Is there any way to accomplish this in java? 有什么办法可以在Java中做到这一点?
Try this, 尝试这个,
JSONObject obj= new JSONObject();
String title = "gro";
String startExpr = "/.*";
String endExpr = ".*/";
obj.put("title", startExpr + title + endExpr );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.