I've now done the following - anything wrong with that?

                
                <cfset obj = StructNew()>
                <cfset obj.total = list.recordcount>
                <cfset obj.items = ArrayNew(1)>
                <cfloop query="list">
                        <cfset item = StructNew()>
                        <cfset item.id = #fileid#>
                        <cfset item.title = #entered_name#>
                        <cfset ArrayAppend(obj.items,item)>
                </cfloop>
                
                <cfset jsondata = serializeJSON(obj)>
                <cfoutput>#jsondata#</cfoutput>


Regards,

Stefan







On 9 Oct 2009, at 17:10, Stefan Richter wrote:

>
> Brilliant, thanks.
>
> I'll give that a shot.
>
> Cheers
>
> Stefan
>
>
>
>
> On 9 Oct 2009, at 16:59, [email protected] wrote:
>
>>
>> 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 <[email protected]>
>> Date: Fri, October 09, 2009 10:39 am
>> To: cf-talk <[email protected]>
>>
>>
>> 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:327071
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to