user3239126
user3239126

Reputation:

grabbing JSON data using coldfusion

I have a URL which when run in the browser, displays JSON data, since I am new to coldfusion, I am wondering, what would be a good way to grab the data from the web browser? Later on I will be storing the individial JSON data into MySQL database, but I need to figure out step 1 which is grabbing the data.

Please advise.

Thanks

Upvotes: 0

Views: 971

Answers (2)

nkostic
nkostic

Reputation: 595

Here is quick example:

<!--- Set the URL address. --->
<cfset urlAddress="http://ip.jsontest.com/">
<!--- Generate http request from cf --->
<cfhttp url="#urlAddress#" method="GET" resolveurl="Yes" throwOnError="Yes"/>
<!--- handle the response from the server ---> 
<cfoutput>
  This is just a string:<br />
  #CFHTTP.FileContent#<br />
</cfoutput>
<cfset cfData=DeserializeJSON(CFHTTP.FileContent)> 
This is object:<br />
<cfdump var="#cfData#">
Now you can do something like this:<br />
<cfoutput>#cfData.ip#</cfoutput> 

Execute this source here http://cflive.net/

Upvotes: 3

CreativeNotice
CreativeNotice

Reputation: 236

  1. You'll want to do a cfhttp request to load the external content.
  2. Then you can use deserializeJSON to convert the JSON object into the appropriate cfml struct.

See the example Adobe gives in the deserializeJSON documentation.

Upvotes: 5

Related Questions