有没有办法在coldfusion中轻松地将cfquery输出转换为DataTable JSON格式?

时间:2013-01-22 19:45:03

标签: coldfusion google-visualization

我正在尝试使用Google Charts API生成图表,但我似乎无法以所需格式获取数据。

coldfusion中的

serializeJSON(data)为我提供了JSON数据,但看起来并不是Charts API所期望的。我可以使用任何组件将其转换为所需的格式吗?

查询

<cfquery name="getAllUsers" datasource="cccac_swipe" result="UserDetailsResult">
Select login_mode,count(login_mode) as total from login_activity,ccac_registered_users where login_activity.student_id=ccac_registered_users.student_id GROUP BY login_mode
</cfquery>

ColdFusion的JSON

{"COLUMNS":["LOGIN_MODE","TOTAL"],"DATA":[["manual",123],["swipe",20]]}

预期的JSON

{ "cols": [ {"id":"","label":"SignIn Method","pattern":"","type":"string"}, {"id":"","label":"Count","pattern":"","type":"number"} ], "rows": [ {"c":[{"v":"manual","f":null},{"v":123,"f":null}]}, {"c":[{"v":"swipe","f":null},{"v":20,"f":null}]} ] }

1 个答案:

答案 0 :(得分:5)

这基本上就是你所需要的。只需插入您的查询数据即可。

<cfset chartsData = structNew()>
<cfset chartsData["cols"] = arrayNew(1)>

<!--- use a query loop to copy query data to this struct --->
<cfloop query="UserDetailsResult">
   <cfset chartsRow = structNew()>
   <cfset chartsRow["id"] = ""> 
   <cfset chartsRow["label"] = "SignIn Method">
   <cfset chartsRow["pattern"] = "">
   <cfset chartsRow["type"] = "string">

   <cfset arrayAppend(chartsData["cols"], chartsRow)>
</cfloop>

<cfset chartsDataJSON = serializeJSON(chartsData)>