基本上我有一个来自数据库的对象,我将其设置为字符串并传递给Web服务。
这很好用。但是我想从数据库中对该字符串进行硬编码以进行一些单元测试。
问题是......字符串在WS中必需的某些地方有“”。我知道我可以手动将所有这些更改为'',然后将整个字符串括在“”中,但有没有一个实用程序可以为我做这个?
string test = {
"testMap": {
"test1": "",
"test2": "blah",
}
}
答案 0 :(得分:0)
你可以尝试寻找一个Java库来编码JSON,这可能是更好的长期策略。
如果您只是在谈论格式化该字符串,只需执行查找(")并替换(\")以逃避"?
String test = "{ " +
"\"testMap\": { " +
"\"test1\": \"\", " +
"\"test2\": \"blah\", " +
" }" +
"}";
//output
{ "testMap": { "test1": "", "test2": "blah", }}
答案 1 :(得分:0)
使用org.apache.commons.lang.StringEscapeUtils
String output = org.apache.commons.lang.StringEscapeUtils.escapeJava(input)
答案 2 :(得分:0)
尝试修改此python脚本,它会从文本中删除所有空格。
import re
x = u"abc de\t \tf"
print x # abc de f
y = re.sub(r"\s+", u",", x)
print y # abc,de,f