0

When I try to stringyfy

ABC
<a href="abc.co.dds">
 dfsdsf
</a>

JSON gives me

"ABC\n<a href=\"abc.co.dds\">\n dfsdsf\n</a>"

but when I try

s=('"ABC\n<a href=\"abc.co.dds\">\n dfsdsf\n</a>"');
JSON.parse(s)

I get a SyntaxError: Unexpected token on my console

How can I parse a manually entered string with JSON?

1
  • I'm confused as to why this is tagged as Python - the code doesn't look like Python (why the semicolon? And the Python JSON module is json). Commented May 1, 2012 at 15:54

1 Answer 1

1

From the JSON specification (second 2):

A JSON text is a serialized object or array.

Since you are starting with a string (and not an object or an array), a JSON serializer should (IMO) throw an exception instead of giving you an escaped string.

You can work around your problem by wrapping your string in { "data": your_string } before converting to JSON (using whatever syntax that language you are using supports for objects/hash maps/associative arrays/etc). Obviously, you'll need to access foo.data after parsing the JSON to a native object at the other end).

Sign up to request clarification or add additional context in comments.

Comments