以@开头的变量名称导致错误:在X行的Y列发现无效的标记@

时间:2018-09-09 20:10:00

标签: xml coldfusion weather-api

我正在尝试使用来自国家气象局API(NOAA)的数据格式化ColdFusion页面。使用以下ColdFusion代码,我可以转储数据。

<cfhttp url="https://api.weather.gov/alerts/active/zone/ANZ335" result="weather">
<cfhttpparam name="accept" type="header" value="application/ld+json">
</cfhttp>
<cfset alerts = deserializeJSON(#weather.filecontent#) /> 

在转储中,我可以使用“ @”字符命名节点的XML数据。 Dump of XML Data

但是,当您尝试输出值时,ColdFusion不喜欢@符号。例如:

#arraylen(alerts.@graph)#

导致“在X行上发现无效的令牌@ ...” 错误。

关于如何解决该问题的任何想法?

1 个答案:

答案 0 :(得分:3)

使用点符号时,变量名称必须遵守CF's variable naming rules。特别是:

  

变量名称必须以字母,下划线或Unicode开头   货币符号....

对于具有无效变量名的结构键,请使用关联数组表示法:

structName["keyNameInQuotes"] 

...或更具体地说:

 alerts["@graph"]

@Shawn mentioned in the comments一样,您也可以将点符号与关联数组符号混合。因此这些也是有效的:

variables.alerts["@graphs"]
variables["alerts"]["@graphs"]