开发者

AJAX, setJSONStructKey Issues with multiple sets of data

开发者 https://www.devze.com 2022-12-13 18:40 出处:网络
I\'m working to create an autosuggest and am having problems getting the data to display correctly for the JQUERY plugin.

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 respons开发者_如何学JAVAe 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号