我正在尝试使用来自国家气象局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#) />
但是,当您尝试输出值时,ColdFusion不喜欢@
符号。例如:
#arraylen(alerts.@graph)#
导致“在X行上发现无效的令牌@ ...” 错误。
关于如何解决该问题的任何想法?
答案 0 :(得分:3)
使用点符号时,变量名称必须遵守CF's variable naming rules。特别是:
变量名称必须以字母,下划线或Unicode开头 货币符号....
对于具有无效变量名的结构键,请使用关联数组表示法:
structName["keyNameInQuotes"]
...或更具体地说:
alerts["@graph"]
与@Shawn mentioned in the comments一样,您也可以将点符号与关联数组符号混合。因此这些也是有效的:
variables.alerts["@graphs"]
variables["alerts"]["@graphs"]