2

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

serializeJSON(data)在 Coldfusion 中给了我 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}]} ] }
4

1 回答 1

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)>
于 2013-01-23T02:02:59.897 回答