我正在努力创建一个自动提示,但是在为JQUERY插件正确显示数据时遇到了问题。
我的桌子上摆满了customers(customerID,fName,lName)
我需要像这样创建一个JSON响应:
[{"id":"856","name":"House"},
{"id":"1035","name":"Desperate Housewives"},
{"id":"1048","name":"Dollhouse"},
{"id":"1113","name":"Full House"}
]
现在我在Coldfusion中的代码看起来像这样:
Query - sel_Customers.cfm
<cfset newAjax.setJSONStructKey(key="id", value="#sel_Customers.customerID#")>
<cfset newAjax.setJSONStructKey(key="name", value="#sel_Customers.fName#")>
这导致回复:
[{"returnmessage":"The Ajax operation was successful.","name":"Howard","id":"1","returncode":"0"}]
哪个只显示1个记录而不是3.在Coldfusion中是否有办法设置JSONStructKey,如上所示并粘贴?
由于
答案 0 :(得分:3)
我不确定你的setJSONStructKey到底是什么,但是假设你需要结构数组。
硬编码示例:
<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>
因此输出如下:
[{"id":856.0,"name":"House"},{"id":1035.0,"name":"Desperate Housewives"}]
使用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>
注意:请注意this floating numbers issue与SerializeJSON。