Back in July I shared the results of my personal forray into JSON as a blog post, comparing it to XML as a means of returning Ajax data. In order to conduct my experiments, I had utilized an open source CFC called JSON.CFC in order to do the conversion from CF data types to JSON and vica versa. After the post, one individual made the comment that once CF 8 came out, there wouldn't be a need for JSON.CFC anymore. Well, until today, I would have agreed, but now I'm not so sure.
I'm working on a project where I want to retrieve some data sets and keep them client side for fast lookups, so my approach was to return the data to my template as a JSON string, and then just 'pop' it into Javascript, like so:
var seriesgroups = <cfoutput>#seriesgroups#</cfoutput>;
</script>
FYI, the my data has these fields: SERIESGROUPID,NAME
What I expected was that, once I did this, I would be able to access my values in JS like this:
<input type="button" value="test" onClick="alert(seriesgroups.data.name[0]);" />
...
My data was converted from a query to JSON using CF8's new "serializeJSON()" function. But to my mild irritation, I was not able to access the data as expected. I thought to myself, "self, this is an odd thing, because when I performed the same task using the JSON component a few months ago, it worked fine". Just to double check myself, I re-read my blog post on JSON and decided to use JSON.CFC to see if the results were the same. Using JSON.CFC, I was able to get the expected results.
So here's the thing, then: JSON.CFC, MAYBE YOU'RE NOT SO OBSOLETE AFTER ALL!
Or, it could also be that I am just not adept enough at reading JSON strings and can't figure out the appropriate way to access my JS object in such a way that I could easily loop over it looking for specific values. If anyone knows how one SHOULD go about accessing data that was serialized using "serializeJSON" in a JS context, I would love to see an example! I could probably figure it out if I had the luxury of time, but alas, I do not.
Okay, just to top things off with a side by side comparison (which I love doing), here are the results of the CF 8 and JSON.CFC conversions:
| Method | JSON String |
|---|---|
| JSON.CFC | {"recordcount":1,"columnlist":"name,seriesgroupid","data":{"name":["Exports"],"seriesgroupid":[1]}} |
| serializeJSON() | {"COLUMNS":["SERIESGROUPID","NAME"],"DATA":[[1,"Exports"]]} |
| JSON.CFC dump | serializeJSON dump |
|---|---|
![]() |
![]() |
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
Eg: <cfset struct["foo"] = value />

