带有撇号的JSON值 [英] JSON value with apostrophe
问题描述
我有一个rel属性包含JSON字符串的元素,例如:
I have an element with a rel attribute that contains a JSON string, something like:
rel='{"id":"#id#","name":"#name#"}'
<在我的javascript代码中,我使用 $。parseJSON
来解析这些数据。这正确工作 - 除了 name
包含撇号的情况。我尝试使用 jsStringFormat
,coldfusion 替换
,替换所有单引号转义单引号等,但我似乎无法打上正确的解决方案。我知道这可能很简单,但是如何让代码使用json用撇号/单引号正确地传递值?
Then, in my javascript code, I use $.parseJSON
to parse this data. This works correctly - besides for cases where name
contains an apostrophe. I've tried using jsStringFormat
, a coldfusion replace
that replaces all single quotes with escaped single quotes, etc, but I can't seem to hit on a correct solution. I know this is probably simple, but how do I get the code to correctly pass values with apostropes/single quotes using json?
这段代码工作,但是消除了撇号我想保留:
This code works, but eliminates the apostrophes which I'd like to preserve:
rel='{"id":"#id#","name":"#replace(name,"'","","all")#"}'
不工作:
rel='{"id":"#id#","name":"#replace(name,"'","\'","all")#"}'
rel='{"id":"#id#","name":"#replace(name,"'","\\\'","all")#"}'
或:
rel='{"id":"#id#","name":"#replace(name,"'",""","all")#"}'
:
rel='{"id":"#id#","name":"#jsStringFormat(name)#"}'
推荐答案
让这个工作:)
rel='{"id":"#id#","name":"#replace(name,"'","&##39;","all")#"}'
这篇关于带有撇号的JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!