代码之家  ›  专栏  ›  技术社区  ›  Stewart Robinson

如何绕过CF8编码不可打印字符的serialijson?

  •  3
  • Stewart Robinson  · 技术社区  · 15 年前

    serialijson创建带有不可打印字符(即ascii 21)的json

    这是无效的JSON。我怎样才能绕过这个问题。

    正则表达式删除不可打印字符是否有效?

    什么regex可以删除不可打印的字符?

    1 回复  |  直到 15 年前
        1
  •  4
  •   Sergey Galashyn    15 年前

    嗯,这个简单的解决方案是为cffeed创建的,但是您的问题非常相似。

    首先,我尝试使用Java库StReStudioUnIdILS(公钥LANAPI),但是它没有正确地从我的内容中逃脱。尽管它是为XML推荐的。

    所以,这个cfc方法对我有效。也许也能帮到你。

    <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>