简体   繁体   中英

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.

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. Which library you are using..?

Also try returning the original jsonObject itself than the one converted with toString . 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
return StringEscapeUtils.unescapeJava(resultSet.toString()) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM