I have a string in this format:
var abc = "{'ABC':'25117', 'data':'India\"NewDelhi\"'}"
I want to convert this to a JSON object.
I've tried to use "" and '' by interchanging them.
I've used the function JSON.parse(abc), it works if the string
var abc = '{"ABC":"25117", "data":"India\'NewDelhi\'"}'
But I want that "NewDelhi" should be in double quotes i.e. "" and this is my strict condition.
Chamika Sandamal
24.4k5 gold badges68 silver badges88 bronze badges
asked Aug 18, 2014 at 12:59
Chitresh Sinha
673 silver badges8 bronze badges
-
1Your strict condition means that the string you have is not valid JSON, and it can't be converted. Though luck, either drop that silly condition or create your own parser for you invalid format.adeneo– adeneo2014年08月18日 13:02:08 +00:00Commented Aug 18, 2014 at 13:02
-
That JSON isn't valid. Use jsonlint in the future.Ben– Ben2014年08月18日 13:08:42 +00:00Commented Aug 18, 2014 at 13:08
2 Answers 2
Stick with valid JSON, and just double escape the quotes
var abc = '{"ABC":"25117", "data":"India\\"NewDelhi\\""}';
answered Aug 18, 2014 at 13:04
adeneo
319k29 gold badges410 silver badges392 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You have to escape "\" to be able to parse your string.
var abc = "{\"ABC\":\"25117\", \"data\":\"India\\\"NewDelhi\\\"\"}"
Then JSON.parse(abc) will works
answered Aug 18, 2014 at 13:07
Aguardientico
7,7991 gold badge36 silver badges34 bronze badges
Comments
lang-js