4

With the following code I am creating an object to pass to a webservice. The single quote seems to be handled with a replace in the stringify before the object is passed to the service, but I don't know how to modify the double quote. If I inspect the Program.Comments field it would show the string like \"Word". This will cause an error in the stringify.

Is this an incorrect way to use the stringify for Json

 Program = new Object();
 Program.Field1 = $('#txtField1').val();
 Program.Field2 = $('#ddlField2').val();
 Program.Field3 = $('#lblField3').text();
 Program.Field4 = $('#ddlField4').val();
 Program.Field5 = $('#ddlField5').val();
 Program.Field6 = $('#ddlField6').val();

 // This field may contain both single and double quotes
 Program.Comments = $('#txtComments').val();

 Program.Field7 = $('#txtField7').val();
 Program.Field8 = $('input[name=chbField8]').is(':checked');

 // This will fix the issue of a single quote
 vdata = JSON.stringify(Program).replace(/'/g, "\\'");d
Tom S
  • 227
  • 1
  • 6
  • 19
  • Treat JSONification as opaque. That is, don't attempt to modify JSON strings. If we trust that `JSON.stringify` returns valid JSON (it does), then the issue is on the receiving end not parsing JSON correctly. – amphetamachine Sep 08 '14 at 18:11
  • I have a deserialize function that the json is passed to and this is where it seems to break down. The string would look like: {"ProgramComments":"It's in "Current"","ProgramStartDate":"8/5/2006","ProgramActive":true} – Tom S Sep 08 '14 at 18:39
  • That's not a possible value for `JSON.stringify` to produce -- it's invalid JSON. Did you overwrite the stringify method to make it produce garbage JSON? Also, did you write the deseralization method yourself? – amphetamachine Sep 08 '14 at 21:23
  • Here's normal JSON.stringify output: `Program = new Object(); Program.foo = "Bar's \"Baz\" Quux"; JSON.stringify(Program)` => `"{"foo":"Bar's \"Baz\" Quux"}"` – amphetamachine Sep 08 '14 at 21:25
  • If I remove the .replace function, the stringify will fail, and when inspecting the Program object, the Program.Comments is declared a non json string? So why will the stringify not handle my above object Program.Comments that may contain both an apostrophe and double quotes? Thank is my problem. – Tom S Sep 09 '14 at 12:26
  • Again, I'm confident JSON.stringify produces good output, regardless of whether the data it serializes contains single or double quotes. I think your problem lies in the deserialization method. What are you using for deserialization? – amphetamachine Sep 09 '14 at 13:56
  • The following is what I am seeing from stringify just before it enters the deserialize the type {"ProgramComments":"It is "Current"", (notice the spare") "ProgramStartDate":"8/5/2006", "ProgramActive":true}. The ProgramComments has no escape characters around the word "Current" when it should, based on your above description. – Tom S Sep 09 '14 at 15:04
  • Something is happening to the data along the way because that's not valid JSON. Can you verify that JSON.stringify generates valid JSON? – amphetamachine Sep 09 '14 at 15:15
  • I found a piece of code for replace(/(['"])/g,"\\$1") that seems to work. Thank you for your persistence on this. – Tom S Sep 09 '14 at 18:09

0 Answers0