如何在 CF8 编码不可打印字符中绕过 SerializeJSON? [英] How to get around SerializeJSON in CF8 encoding non printable characters?

查看:19
本文介绍了如何在 CF8 编码不可打印字符中绕过 SerializeJSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SerializeJSON 创建带有不可打印字符的 JSON(即 ASCII 21)

SerializeJSON creates JSON with non printable characters (i.e. ASCII 21)

这是无效的 JSON.我怎样才能解决这个问题.

This is invalid JSON. How can I get round this.

删除不可打印字符的正则表达式会起作用吗?

Would a regex removing the non printable characters work?

什么正则表达式会删除不可打印的字符?

What regex would remove non printable characters?

推荐答案

嗯,这个简单的解决方案是为 cffeed 创建的,但是你的问题很相似.

Well, this simple solution was created for cffeed, but your problem is very similar.

首先我尝试使用 Java 库 StringEscapeUtils (Commons Lang API),但它没有正确地转义我的内容.虽然推荐用于 XML.

First I've tried to use Java library StringEscapeUtils (Commons Lang API), but it didn't escaped my contents properly. Though it is recommended for XML.

所以,这种 cfc 方法对我有用.也许也会对你有所帮助.

So, this cfc method works for me. Maybe will help you too.

<cffunction name="cleanXmlString" access="public" returntype="any" output="false" hint="Replace non-valid XML characters">
    <cfargument name="dirty" type="string" required="true" hint="Input string">
    <cfset var cleaned = "" />
    <cfset var patterns = "" />
    <cfset var replaces = "" />

    <cfset patterns = chr(8216) & "," & chr(8217) & "," & chr(8220) & "," & chr(8221) & "," & chr(8212) & "," & chr(8213) & "," & chr(8230) />
    <cfset patterns = patterns & "," & chr(1) & "," & chr(2) & "," & chr(3) & "," & chr(4) & "," & chr(5) & "," & chr(6) & "," & chr(7) & "," & chr(8) />
    <cfset patterns = patterns & "," & chr(14) & "," & chr(15) & "," & chr(16) & "," & chr(17) & "," & chr(18) & "," & chr(19) />
    <cfset patterns = patterns & "," & chr(20) & "," & chr(21) & "," & chr(22) & "," & chr(23) & "," & chr(24) & "," & chr(25) />
    <cfset patterns = patterns & "," & chr(26) & "," & chr(27) & "," & chr(28) & "," & chr(29) & "," & chr(30) & "," & chr(31) />

    <cfset replaces = replaces & "',',"","",--,--,..." />
    <cfset replaces = replaces & ",-, , , , , , , " />
    <cfset replaces = replaces & ", , , , , , " />
    <cfset replaces = replaces & ", , , , , , " />
    <cfset replaces = replaces & ", , , , , , " />

    <cfset cleaned = ReplaceList(arguments.dirty, patterns, replaces) />

    <cfreturn cleaned />

</cffunction>

这篇关于如何在 CF8 编码不可打印字符中绕过 SerializeJSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆