我在使用ColdFusion和JSON时遇到了一些问题。我的用户有文件名和其他关键字,其中包含ç
等字符,当我必须通过JSON传回它们时,这会让我感到痛苦。
当我在变量上使用magic JSON命令时:
<cfcontent type="application/json">
<cfset variables.stGalleryItem = StructNew() />
<cfset variables.stGalleryItem["imagePath"] = siteRoot & '/images/350460/hellç.txt' />
<cfset variables.stGalleryItem["title"] = 'çççç' />
<cfset variables.stGalleryItem["author"] = 'HI' />
<cfset variables.stGalleryItem["text"] = 'aa' />
<cfset ArrayAppend(variables.arrGallery,variables.stGalleryItem) />
<cfoutput>
#Trim(SerializeJSON(variables.arrGallery))#
</cfoutput>
吐出的角色是 ,没有任何好处。
我可以做些什么来保护我的用户ç
?
答案 0 :(得分:8)
您需要在CFCONTENT标记中指定字符集。我在没有字符集的情况下在Google Chrome中尝试了此代码,它正确地返回了文本。但是,FireFox 3.6返回了您列出的错误字符。
这正确地返回Chrome,FireFox和MSIE中的UTF-8字符:
<cfcontent type="application/json; charset=utf-8">
答案 1 :(得分:0)
自己进行转换:http://tojson.riaforge.org/(本机)或http://json-lib.sourceforge.net/(通过Java库)