Java String to Object getting unwanted '\' -
i'm building mongodb java query, , while trying find specific value in db, query query gets messed up, because of unwanted '\' chars in string. here i'm doing.
i list of strings mongodb, , perform find in collection, using strings.
dbcursor = null; for(string s : list){ basicdboject query = = new basicdbobject("actor.preferredusername", s); cursor = coll.find(query); //treat results } if system.out.println() using query, get:
{ "actor.preferredusername" : "\"napolesunset75\""} notice '\' in last field.
now, test purposes, if change 's' fixed string "xpto", query created in sucessfully, returning
{ "actor.preferredusername" : "xpto"} can help?
i think, due quote inside string going store in database. if that's can use class , it's static public method validate string before storing in database :
public class sqlvalidator { private static final string single_quote = "\u0027"; /** * replaces ' ''. should used in read/write queries. * * @param str */ public static string quoteinquery(string str) { if (str == null) return null; return str.replaceall(single_quote, single_quote + single_quote); } }
Comments
Post a Comment