Personally, I don't fight it and just accept the default serialization
of ColdFusion's queries.  They usually some across as an ArrayList in
ActionScript and checking the length property will tell you what the
total number of records are.  It's easier to just use the meta data of
the object itself then to try and represent it explicitly.

Regardless, the answer to your question is this:  If you want the data
represented in a custom way, then build it.  What you have is a struct
with two keys.  One called "total" and one called "items".  Remember
that CF will capitalize all struct keys when made like this
myStruct.total = "foo" so you will want to use array notation: 
mystruct["total"] = "foo".  Inside of your "items" key you have an array
of structs containing the keys "id" and "title".

So, loop over your CF query and create just that then serialize it to
JSON.  If I may point out, ColdFusion's method of serializing a result
set is more efficient than yours.  You will require each column name to
be stated as a struct key for every row instead of simply defining the
columns once.  Imagine a result set with 100 records and 10 columns.  If
the average column title is 8 characters, that's 8000 extra characters
going down the wire.  It might be negligible, but I'm just sayin'  :)

This is what I'd start with (in cfscript):


returnStruct = {};
returnStruct["total"] = qryResult.recordCount;
returnStruct["items"] = [];

i = 0;

while (++i < qryResult.recordCount)
        {
                thisRow = {};
                thisRow["id"] = qryResult["id"][i];
                thisRow["title"] = qryResult["title"][i];
                arrayAppend(returnStruct["items"],thisRow);
        }
        
return serializeJSON(returnStruct);

Good Luck.

~Brad

-------- Original Message --------
Subject: JSON help
From: Stefan Richter <ste...@flashcomguru.com>
Date: Fri, October 09, 2009 10:39 am
To: cf-talk <cf-talk@houseoffusion.com>


Hi,
Flex dev here - wannabe CF coder :-)

I need to return some JSON to a Flash Lite client, the format I'd like 
to produce looks like this:

{
 "total": "100",
 "items":
 [
 {"id": "0", "title": "Some Title"},
 {"id": "0", "title": "Some Title"},
 {"id": "0", "title": "Some Title"},
 {"id": "0", "title": "Some Title"},
 {"id": "0", "title": "Some Title"}
 ]
}



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:327067
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to