...using JSON2.js and JQUERY
as you can see from the first image the object property customerReport.Title has an apostrophe. In the code you can see that I'm calling JSON.stringify() into reportAsJson string which still has the unescaped apostrophe.
the error returned by $.ajax() is {"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. ...
Initially I'm just going to ban apostrophe's from the user, but I thought JSON.stringify() handled this or do I need to set some option????
Thanks
JSON. stringify does not act like an "identity" function when called on data that has already been converted to JSON. By design, it will escape quote marks, backslashes, etc. You need to call JSON.
To use an apostrophe or a single quotation mark (') in a value, add another single quotation mark after the first one. Notice that the value for name in JSONCUSTOMER table is "O''HARA". The name value in JSONCUSTOMER table is saved as O'Hara.
Use escapeEcmaScript method from Apache Commons Lang package: Escapes any values it finds into their EcmaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.). So a tab becomes the characters '\\' and 't' .
JSON strings do not allow real newlines in its data; it can only have escaped newlines. Snowflake allows escaping the newline character by the use of an additional backslash character.
You can avoid removing these apostrophes replacing them with an HTML entity '
- that's a single quot - and later decode HTML entities either in the client or server-side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With