ColdFusion访问嵌套结构体问题:Azure DevOps API字段引用报错
解决ColdFusion中访问Azure DevOps API返回的带点字段名问题
正确的直接引用方法
当Azure DevOps API返回的结构体字段名包含点(如System.AreaPath、System.TeamProject)时,ColdFusion中需要用引号包裹键名来访问,以下是两种常用写法:
- 直接方括号+引号访问
<!--- 直接获取单个字段值 ---> <cfset areaPath = result.fields["System.AreaPath"]> <cfset teamProject = result.fields["System.TeamProject"]> <!--- 输出示例 ---> Area Path: #areaPath#<br> Team Project: #teamProject#
- 使用
structFind函数获取
<cfset areaPath = structFind(result.fields, "System.AreaPath")> <cfset teamProject = structFind(result.fields, "System.TeamProject")>
为什么你的写法报错
你尝试的#result.fields[f].[System.AreaPath]#存在两个问题:
System.AreaPath本身就是result.fields结构体下的直接键,并非f对应的子结构体字段- 带特殊字符(如点)的键名必须用引号包裹,否则ColdFusion会将点解析为结构体层级分隔符,导致找不到对应字段
遍历所有字段的示例
如果需要遍历返回的所有字段,可以用cfloop遍历结构体集合:
<cfloop collection="#result.fields#" item="fieldKey"> <cfoutput> #fieldKey#: #result.fields[fieldKey]#<br> </cfoutput> </cfloop>
内容的提问来源于stack exchange,提问作者lollavel
相关产品推荐
相关产品推荐

