arrays - Converting JSON data into a ColdFusion struct -
the following code have works treat. taking data sql database table , outputting structure below within cfc, output called mobile app, , code works required.
<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>
i have data coming webservice returned page in json format. want replicate above cfc function can output json data in exact same way shown above. webservice outputs data below
<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#">
having spent many hours researching how achieve above need concede need assistance far more experienced them myself. need able generate same structure have using json content have sql query (yes appreciate column names different)
i thank in advance assistance can provided.
you can loop on results build structure.
<cfloop array="#data.results#" index="x"> // build 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>
Comments
Post a Comment