0

My JSON that is being returned from my ASP.NET MVC application looks like this:

{code: "1", error: "0", output: "<div class="a1"><div class="b1">this is fasta's</div></div>}

It is not working because I am not escaping it properly.

I'm not using a JSON library, can someone recommend a function that would clean up my HTML so this works?

I tried escaping for \ but it still doesn't work.

If I HTML encode it, should it work fine?

There will be user generated content so this has to work for all potential inputs by the user.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • Duplicate of http://stackoverflow.com/questions/983451/list-of-escape-characters-required-for-my-json-ajax-return-type. – Levi Jun 11 '09 at 23:28

2 Answers2

1

Are you able to use single quotes to wrap your HTML attribute values? I think that should work if you are able to do that.

For example,

{code: "1", error: "0", output: "<div class='a1'><div class='b1'>this is fasta's</div></div>"}

If that doesn't work, try using 2 backslashes to escape the double quotes.

For example,

{code: "1", error: "0", output: "<div class=\\"a1\\"><div class=\\"b1\\">this is fasta's</div></div>"}
Tim Banks
  • 7,099
  • 5
  • 31
  • 28
0

You need to escape the internal double quotes, like this:

{code: "1", error: "0", output: "<div class=\"a1\"><div class=\"b1\">this is fasta's</div></div>"}
Chad
  • 1,750
  • 2
  • 16
  • 32