JSON value with an apostrophe

I have an element with a rel attribute that contains a JSON string, something like:

rel='{"id":"#id#","name":"#name#"}'

Then in my javascript code I use $.parseJSONto analyze this data. This works correctly - except when it namecontains an apostrophe. I tried using jsStringFormatcoldfusion replace, which replaces all single quotes with escaped single quotes, etc., but I can't find the right solution. I know this is probably simple, but how do I get the code to correctly pass values ​​using apostropes / single quotes using json?

This code works, but removes the apostrophes I would like to keep:

rel='{"id":"#id#","name":"#replace(name,"'","","all")#"}'

This does not work:

rel='{"id":"#id#","name":"#replace(name,"'","\'","all")#"}'

Doesn't:

rel='{"id":"#id#","name":"#replace(name,"'","\\\'","all")#"}'

Or:

rel='{"id":"#id#","name":"#replace(name,"'",""","all")#"}'

Or:

rel='{"id":"#id#","name":"#jsStringFormat(name)#"}'
+5
source share
3

, :)

rel='{"id":"#id#","name":"#replace(name,"'","&##39;","all")#"}'
+4

, , , ​​ . , .

JSON:

JSON - SerializeJSON, ColdFusion JSON.

, :

rel='#SerializeJSON({"id"=Variables.id,"name"=Variables.name})#'

HTML:

, , , , html.

ColdFusion 10 EncodeForHTMLAttribute.

rel='#EncodeForHTMLAttribute(SerializeJSON({"id"=Variables.id,"name"=Variables.name}))#'

- CF10, ESAPI-. ( ColdFusion)

rel='#CreateObject("java", "org.owasp.esapi.ESAPI").encoder().encodeForHTMLAttribute(SerializeJSON({"id"=Variables.id,"name"=Variables.name}))#'

CFC ESAPI- CF9, CreateObject .

+2

In JavaScript, avoid single quotes in strings with \.

In HTML, you should use double quotes for attributes and avoid double quotes, for example:

rel="{"id":"#id#","name":"#name#"}"
+1
source

All Articles