user34537
user34537

Reputation:

JSON not allowing :\

Demo

alert('How do i make abspath==U:\\path?'); //shouldnt i only need one \?
alert(JSON.stringify({abspath:'U:\path'})); //wtf
alert(JSON.stringify({abspath:'U:\\path'}));//wtf2
//alert(JSON.stringify({abspath:'U:/path'}));//different
alert(JSON.stringify({abspath:"U:\path"}));  //even here!?
alert(JSON.stringify({abspath:"U:\\path"})); //fuuuuuuuuuuuuu

Upvotes: 1

Views: 245

Answers (4)

Ryan Caskey
Ryan Caskey

Reputation: 607

The string stored in the object:

JSON.parse(JSON.stringify({"abspath": "U:\\path"})).abspath 

...has only one \.

Upvotes: 2

Chris Baker
Chris Baker

Reputation: 50592

When the script outputs {"abspath":"U:\\path"}, that is a valid JSON string. It doesn't look like a valid object because it is still escaped -- JSON string aren't intended to be human-readable.

If you were to decode that string, you would end up with the desired value. Your output is still escaped, as it should be, pending decoding. If it was NOT escaped in the encoded string, you wouldn't be able to decode it.

See happen: http://jsfiddle.net/dhzMQ/1/ (requires availability of console)

Further Reading

Upvotes: 4

tomdemuyt
tomdemuyt

Reputation: 4582

alert( JSON.stringify({abspath:"U:\\path"}) )

This is corrrect, you need \\ in JSON format because that is how \ is stored.

You can tell by parsing that JSON and querying abspath.

alert(JSON.parse(JSON.stringify({abspath:"U:\\path"})).abspath); 
alert( JSON.stringify({abspath:"U:\\path"}) )

Upvotes: 3

Adam Jenkins
Adam Jenkins

Reputation: 55613

valid JSON has quotes around the key and the value

{"abspath":"U:\\path"}

Works for me

JSON.stringify({"abspath":"U:\\path"});

Upvotes: 1

Related Questions