2015-06-20 151 views
0

下面的代码我有像一个款待作品。它从我的SQL数据库表中取出数据并将其输出到cfc中的下面的结构中,输出从我的移动应用程序中调用,并且代码按需要工作。将JSON数据转换为ColdFusion结构

<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#"> 

已经花了很多时间研究如何实现我要承认,我从那些更需要帮助上述经历他们自己。我只需要能够生成相同的结构,我已经使用JSON内容,因为我有SQL查询(是的,我欣赏列名是不同的)

我在此先感谢您的任何帮助,可以提供。

+0

你最终的目标是什么?似乎你正在采取一种非常迂回的方式来输出查询。 –

+0

嗨马特,最后的目标是能够从web服务输出数据,就像我从查询示例中获得的那样。我编写了一个调用cfc的iphone应用程序,并且cfc返回了初始示例中显示的结构中的数据,应用程序页面然后处理数据并将其显示在应用程序页面的列表中。我很高兴与示例查询的工作方式和它为我的应用程序生成的输出,我只需要从JSON数据输出相同的结构 – user3288814

回答

0

您可以遍历结果来构建结构。

<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> 
+0

嗨Slick 我遵循你的步骤和是它现在输出,但输出重复记录总数相同的记录,而不是显示数据集中下一条记录的详细信息?我欢迎你的想法 – user3288814

+0

尝试我的修改示例。使用重复() – BuzzCloudAU

+0

SlickRick - 好的想法,但它不应该是必要的。循环代码在每次迭代中创建一个新结构,因此它不是[“通过引用传递”](http://stackoverflow.com/a/7617372/104223)问题。 user3288814 - 这表明你的代码有些不同。当你说“输出”时,你的意思是函数的原始*结果*(即转储'结果'变量显示重复值) - 或者当您在其他页面上显示结果时?您能否使用您当前的代码更新您的问题以及从Web服务收到的实际JSON示例(如果需要,请将其混淆)。 – Leigh