2015-06-20 152 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