[英]Using Json with java returns string with \“ instead of ”
Am using jsonobject and json array and converting the final result to string using jsonobject.toString()
function and returning the string to browser. 我正在使用jsonobject和json数组,并使用
jsonobject.toString()
函数将最终结果转换为字符串,并将该字符串返回给浏览器。
Code snippet: 程式码片段:
JSONArray VMs= new JSONArray();
JSONObject resultSet = new JSONObject();
for(Vector<String> stages : mrs)
{
VMs.put(new JSONObject().put("Vm",stages.get(0)));
}
resultSet.append("ServerDetails",(new JSONObject().put("Server",sName).append("Vms", stageNames)));
return resultSet.toString();
output on browser: 在浏览器上输出:
"{\"ServerDetails\":[{\"Server\":\"myserver45\",\"Vms\":[[{\"Vm\":\"vm1022\"},{\"Vm\":\"vm9875\"}]]}]}"
I don't want it to return this way. 我不希望它以这种方式返回。 How do i make this return as follows without slashes-
我如何按如下方式返回此收益-
"{"ServerDetails":[{"Server":"myserver45","Vms":[[{"Vm":"vm1022"},{"Vm":"vm9875"}]]}]}"
I don't understand why "
gets replaced with \\"
everywhere. 我不明白为什么到处都用
"
替换为\\"
。 Please help. 请帮忙。
If you are using net.sf.json
json library, both jsonObject
and jsonObject.toString()
provide output values without string escape character. 如果使用
net.sf.json
json库,则jsonObject
和jsonObject.toString()
提供不带字符串转义符的输出值。 Which library you are using..? 您正在使用哪个库..?
Also try returning the original jsonObject
itself than the one converted with toString
. 还尝试返回原始
jsonObject
本身,而不是返回使用toString
转换的原始jsonObject
。 It might give you the desired result. 它可能会给您想要的结果。
Alternate option would be to unescape the string which is send to browser. 另一种选择是取消发送给浏览器的字符串。 You can use stringescapeutils from
org.apache.commons.lang
for this 您可以为此使用
org.apache.commons.lang
中的stringescapeutils。
return StringEscapeUtils.unescapeJava(resultSet.toString()) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.