0

I have an input hidden element, the value of which contains JSON of an HTML encoded string. Something like

{"QuestionInstruction":"<snippet lang="java"> public class Test() {\n\n}</snippet>","QuestionDescription":"it this correct?","Choices":["True","False"] }

and when I do

var text = document.getElementById('inputId').value, 

the &quot automatically gets converted to "". The value of text comes as -

{"QuestionInstruction":"<snippet lang="java"> public class Test() {\n\n}</snippet>","QuestionDescription":"it this correct?","Choices":["True","False"] }

So, then JSON parsing fails. :(

I am using FF9.

animuson
  • 53,861
  • 28
  • 137
  • 147
saarthak
  • 1,004
  • 2
  • 12
  • 26

1 Answers1

1

I have managed to get around this by using the following function if I know to expect a value that could contain quotes when creating a JSON string/object...

function getJSONFriendlyString(text) {
    return text.replace(/"/g, "\\\"");
}

Hope that helps you

musefan
  • 47,875
  • 21
  • 135
  • 185
  • please see my edited question and let know how your answer could be a solution – saarthak Feb 21 '12 at 15:25
  • @saarthak: So how is the hidden input being set? why are only some quotes shown as `"`? you may need to go to the code that creates the JSON string in the first place and try and resolve from there – musefan Feb 21 '12 at 15:32
  • 1
    Hey @musefan, I didn't do exactly like you said but did html encoding after preparing the JSON. and that worked. Thanks for hinting. :) – saarthak Feb 21 '12 at 16:05