質問

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