“字符串是动态生成的”。
此时添加单引号。
(如果您遇到问题,请发布它的代码,我们可以解决该问题。)
事后使用正则表达式不是理想的解决方案。
更新:现在我们有更多关于你实际在做什么的信息......
这是将 CF 数据转换为 JS 字符串以用于 Highcharts 的代码的概念验证。
这是一个部分解决方案——我还没有完成整个Highcharts API,下面的一些代码只实现了一半,有些部分可能希望以不同的方式完成,但总的来说它应该让你了解它是如何工作的。
如果您(和/或其他任何人)想要获取下面的代码并将其扩展为 highcharts.cfc,我相信这对 CFML 社区可能有用...
代码:
<cffunction name="convertToHighchartsJs" returntype="String" output=false >
<cfargument name="CfData" type="Struct" required />
<cfif NOT StructKeyExists(Arguments.CfData,'Series')>
<cfthrow message="Does not look like Highcharts data" />
</cfif>
<cfreturn "series:#serializeForHighcharts(Arguments.CfData.Series)#" />
</cffunction>
<cffunction name="serializeForHighcharts" returntype="String" output=false >
<cfargument name="Data" type="any" required />
<cfargument name="Name" type="String" optional />
<cfargument name="ParentName" type="String" optional />
<cfset var JsString = "UNABLE TO SERIALIZE" />
<cfif isStruct(Arguments.Data) >
<cfsavecontent variable="JsString"><cfoutput><!---
--->{<!---
---><cfloop item="local.CurItem" collection=#Arguments.Data#><!---
--->,#fixCase(CurItem)#:#serializeForHighcharts(Arguments.Data[CurItem],CurItem)#<!---
---></cfloop><!---
--->}<!---
---></cfoutput></cfsavecontent>
<cfset JsString = rereplace(JsString,'^\{,','{') />
<cfelseif isArray(Arguments.Data) >
<cfsavecontent variable="JsString"><cfoutput><!---
--->[<!---
---><cfloop index="local.CurItem" array=#Arguments.Data#><!---
--->,#serializeForHighcharts(CurItem)#<!---
---></cfloop><!---
--->]<!---
---></cfoutput></cfsavecontent>
<cfset JsString = rereplace(JsString,'^\[,','[') />
<cfelseif isSimpleValue(Arguments.Data) >
<cfset var ShouldBeQuoted = false />
<cfif StructKeyExists(Arguments,'Name')>
<cfif ListFindNoCase('name,text,renderTo,type,color,layout,align,verticalalign',Arguments.Name)>
<cfset ShouldBeQuoted = true />
</cfif>
<cfelseif StructKeyExists(Arguments,'ParentName') AND ListFindNoCase('categories',Arguments.ParentName) />
<cfset ShouldBeQuoted = true />
</cfif>
<cfif ShouldBeQuoted >
<cfset JsString = "'" & JsStringFormat(Arguments.Data) & "'" />
<cfelse>
<cfset JsString = Arguments.Data />
</cfif>
</cfif>
<cfreturn JsString />
</cffunction>
<cffunction name="fixCase" returntype="String" output="false" access="private">
<cfargument name="Text" type="String" required />
<cfif NOT StructKeyExists(Variables,'Camels')>
<cflock type="exclusive" name="regen_camels" timeout=10>
<cfset Variables.Camels = StructNew() />
<cfloop index="CurCamel" list="dataParser,dataURL,legendIndex,xAxis,yAxis">
<cfset Variables.Camels[CurCamel] = CurCamel />
</cfloop>
</cflock>
</cfif>
<cfif StructKeyExists(Variables.Camels,Arguments.Text)>
<cfreturn Variables.Camels[Arguments.Text] />
<cfelse>
<cfreturn Lcase(Arguments.Text) />
</cfif>
</cffunction>
测试:
<cfset CfData =
{ 'Series':
[
{ 'name':'inbounds'
, 'data':
[ ['Date.UTC(2012,4,1)',0]
, ['Date.UTC(2012,4,2)',0]
, ['Date.UTC(2012,4,3)',0]
]
}
,
{ 'name':'outbounds'
, 'data':
[ ['Date.UTC(2012,4,1)',0]
, ['Date.UTC(2012,4,2)',0]
, ['Date.UTC(2012,4,3)',0]
]
}
]
} />
<cfset Expected = "series:[{name:'inbounds',data:[[Date.UTC(2012,4,1),0],[Date.UTC(2012,4,2),0],[Date.UTC(2012,4,3),0]]},{name:'outbounds',data:[[Date.UTC(2012,4,1),0],[Date.UTC(2012,4,2),0],[Date.UTC(2012,4,3),0]]}]" />
<cfset JsString = convertToHighchartsJs(CfData) />
<cfif JsString EQ Expected>
<h1 style="color:green">Matches</h1>
<cfelse>
<h1 style="color:red">different</h1>
</cfif>
<cfdump var=#{Received:JsString,Expected:Expected}# />