views:

27

answers:

1

I'm working to create an autosuggest and am having problems getting the data to display correctly for the JQUERY plugin.

I have a table full of customers(customerID,fName,lName)

I need to create a JSON response like this:

[{"id":"856","name":"House"},
 {"id":"1035","name":"Desperate Housewives"},
 {"id":"1048","name":"Dollhouse"},
 {"id":"1113","name":"Full House"}
]

Right now my code in Coldfusion looks like this:

Query - sel_Customers.cfm

<cfset newAjax.setJSONStructKey(key="id", value="#sel_Customers.customerID#")>
<cfset newAjax.setJSONStructKey(key="name", value="#sel_Customers.fName#")>

This results in the response:

[{"returnmessage":"The Ajax operation was successful.","name":"Howard","id":"1","returncode":"0"}]

Which displays only 1 record and not 3. Is there a way in Coldfusion to setup the JSONStructKey like required and pasted above?

Thanks

+3  A: 

I'm not sure what exactly does your setJSONStructKey, but suppose you need array of structures.

Hardcoded example:

<cfset json = [] />

<cfset customer = {} />
<cfset customer["id"] = "856" />
<cfset customer["name"] = "House" />
<cfset ArrayAppend(json, customer) />

<cfset customer = {} />
<cfset customer["id"] = "1035" />
<cfset customer["name"] = "Desperate Housewives" />
<cfset ArrayAppend(json, customer) />

<cfoutput>
#SerializeJSON(json)#
</cfoutput>

So output will be the following:

[{"id":856.0,"name":"House"},{"id":1035.0,"name":"Desperate Housewives"}]

Using the cfloop:

<cfset json = [] />

<cfloop query="sel_Customers">
    <cfset customer = {} />
    <cfset customer["id"] = sel_Customers.customerID />
    <cfset customer["name"] = sel_Customers.fName />
    <cfset ArrayAppend(json, customer) />
</cfloop>

<cfoutput>
#SerializeJSON(json)#
</cfoutput>

NOTICE: be aware of this floating numbers issue with SerializeJSON.

Sergii