kobe
kobe

Reputation: 15835

json conversion fails due to some characters

The following conversion fails saying bad control character

var myObj = JSON.parse('{"imagePath": "http://somedomain.com/test.jpg?path=\728\1.jpg"}');
console.log(myObj);

is that because of the following characters "\" in the image path , if so do we have any solution for this?

If i remove that character it works.

Thanks.

Upvotes: 0

Views: 124

Answers (2)

Amy
Amy

Reputation: 7466

\ is an escape character. So if you want to use \ in your image Path string, then you need to double escape it. i.e. use \\

Upvotes: 1

Brandon
Brandon

Reputation: 39192

You need to escape the \ character whereever you are generating the JSON.

If it is a literal, then you have to escape the escapes also:

j = JSON.parse('{"imagePath": "http://somedomain.com/test.jpg?path=\\\\728\\\\1.jpg"}');

Upvotes: 1

Related Questions