So I’m working on a pie chart using HighSlide and ColdFusion.
To make this simple, for the data it expects a string like this:
data: [{name: 'Jane',y: 13}, {name: 'John',y: 23}, {name: 'Joe',y: 19}]
What i’ve done to accomplish this is to loop through my query results and create a string like this:
<cfloop query="getAreaCounts">
<cfset areaList = listAppend(areaList, "{name: '#name#',y: #y#}")>
</cfloop>
I know there has to be an easier/smarter way right? Since this is JSON data, i figured I could just do this:
<cfoutput>#SerializeJSON(getAreaCounts)#</cfoutput>
But that returns a JSON string like this which highcharts won’t process:
{"COLUMNS":["Y","NAME"],"DATA":[[8,"Area1"],[7,"Area2"],[1,"Area3"],[1,"Area4"]]}
Any help pointing me in the right direction would be great. Do I need to dig deeper into a JSON – howto?
You’re going to want to convert the query to an array of structs and then run
serializeJSON()on that array.Below is a method that I use pretty frequently when working with a lot of queries and JSON. I think that I got it from Ben Nadel’s site and then converted it to cfscript.. I’ll try to track down the blog post after I post this answer.
So, in your case you would do something like this:
EDIT: Here’s Ben’s blog post that inspired the method above: http://www.bennadel.com/blog/124-Ask-Ben-Converting-a-Query-to-an-Array.htm