문제

I want to put a picture path in a jsonObject and pass it to javascript, in java i do

JSONObject assets = new JSONObject();

assets.put("media",PropertyLoader.getStringValue("PICTURE_DIRECTORY") + "/"+leventPhoto.getFile().getName());

When i get it in the jsp file in javascript it appears like this:

"media":"C:\/Users\/joao\/workspace\/.metadata\/.plugins\/org.eclipse.wst.server.core\/tmp1\/wtpwebapps\/RememberMeServer\/images\/02.jpg"

What can i do to make the separator appear well formated inside JSONObject displayed in javascript?

도움이 되었습니까?

해결책

By the time the data has been read within Javascript, it will have the appropriate name. This is just an escape sequence, which doesn't change the value of the data. For example, in a Javascript console:

var x = "foo\/bar";
var y = "foo/bar";
x == y // true

I'm surprised that the forward slashes are being escaped - just "C:/Users/joao/..." would work fine - but it shouldn't make any difference to the actual value.

다른 팁

It is just escaping the /, it is not changing the string itself.

As to why forward slashes are being escaped, you may want to check out this question:

Allowing / helps when embedding JSON in a tag, which doesn't allow </ inside strings, like [user] points out.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top