本文介绍了有没有办法容易地将cfquery输出转换为ColdFusion中的DataTable JSON格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Google Charts API生成图表,但我似乎无法获取所需格式的数据。
I am trying to generate a chart using Google Charts API, but I can't seem to get the data in the required format.
serializeJSON(data)
在coldfusion中给我的JSON数据,但看起来这不是Charts API的期望。是否有任何组件可以用来将其转换为所需的格式?
serializeJSON(data)
in coldfusion gives me the JSON data, but looks like that is not what the Charts API expects. Is there any component I can use to convert it to the required format?
<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
ColdFusion's JSON
{"COLUMNS":["LOGIN_MODE","TOTAL"],"DATA":[["manual",123],["swipe",20]]}
预期的JSON
Expected 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}]} ] }
推荐答案
只需插入查询数据即可。
This is basically what you need. Just plug in your query data.
<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)>
这篇关于有没有办法容易地将cfquery输出转换为ColdFusion中的DataTable JSON格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!