[英]Converting JSON data into a ColdFusion struct
我下面的代码像对待。 它从SQL数据库表中获取数据并将其输出到cfc中的以下结构中,从我的移动应用程序调用输出,并且代码按要求工作。
<cfset currentRow=1>
<cfloop query="LT_Customers">
<cfset tempData = structNew()>
<cfset tempData["imei"] = LT_Customers.imei[currentRow]>
<cfset tempData["id"] = LT_Customers.id[currentRow]>
<cfset tempData["name"] = LT_Customers.last_name[currentRow]>
<cfset tempData["model"] = LT_Customers.Model[currentRow]>
<cfset tempData["trackee"] = LT_Customers.trackee[currentRow]>
<cfset tempData["mobile"] = LT_Customers.mobile[currentRow]>
<cfset tempData["app_user_mobile"] = LT_Customers.app_user_mobile[currentRow]>
<cfset arrayAppend(result, tempData)>
<cfset currentRow=currentRow+1>
</cfloop>
<cfreturn result>
</cffunction>
现在,我有来自Web服务的数据,这些数据以JSON格式返回到我的页面。 我要做的就是复制上面的内容,以便我的cfc函数可以按照与上述完全相同的方式输出JSON数据。 Webservice输出数据如下
<cfhttp url="http://api.sensis.com.au/v1/test/search?key=czsjp3f8xhd835vg6xfw8ber&query=vetinary%20and%20clinic&radius=1&location=-37.7833,144.9667" method="get" result="httpResp" timeout="120">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
</cfhttp>
<cfset Data=DeserializeJSON(httpResp.filecontent)>
<cfdump var="#Data#">
我花了很多时间研究如何实现上述目标,但我不得不承认,我自己需要经验丰富的人的帮助。 我只需要能够像使用sql查询一样生成使用JSON内容的结构(是的,我很欣赏列名不同)
在此先感谢您提供的任何帮助。
您可以遍历结果以构建您的结构。
<cfloop array="#data.results#" index="x">
// build your results array in here...
<cfset tempData = structNew()>
<cfset tempData["imei"] = x.whatever>
<cfset tempData["id"] = x.whatever>
<cfset tempData["name"] = x.whatever>
<cfset tempData["model"] = x.whatever>
<cfset tempData["trackee"] = x.whatever>
<cfset tempData["mobile"] = x.whatever>
<cfset tempData["app_user_mobile"] = x.whatever>
<cfset arrayAppend(result, Duplicate(tempData))>
</cfloop>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.