Solved

Coldfusion - Json Data Output

Posted on 2010-08-20
4
721 Views
Last Modified: 2012-05-10
Hi Folks,

This is, I'm sure, supposed to be an easy one, but for the life of me I can't figure it out.  Have spent a few hours going through various online resources without success.

Here's what I'm trying to do - grab the Twitter Trends via a cfhttp and output them to a list.  I can successfully grab the Json and Deserialize it, however I keep hitting a block when it comes to the output.

I've pasted my existing code below, any help to put me in the right direction would be much appreciated!
<cfhttp url="http://api.twitter.com/1/trends/current.json"></cfhttp>

<cfset trends = deserializeJSON(cfhttp.FileContent)>



<cfdump var="#trends#">



<cfoutput>

    <cfloop from="1" to="#arrayLen(trends)#" index="i">

    	<a href="" class="bold">#trends.name[i]#</a><br />

    </cfloop>

</cfoutput>

Open in new window

0
Comment
Question by:RedBullLee
  • 3
4 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 33489256
Check this:

<cfhttp url="http://api.twitter.com/1/trends/current.json"></cfhttp>
<cfset current = deserializeJSON(cfhttp.FileContent)>
<cfset trendsName=StructKeyArray(current.trends) >
<cfset trends=current.trends[trendsName[1]] >


<cfoutput>
    <cfloop from="1" to="#arrayLen(trends)#" index="i">
          <a href="" class="bold">#trends[i].name#</a><br />
    </cfloop>
</cfoutput>

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 33489447
Some explanation.
The struct current.trends has one key with keyname that is a date string.
Therefore is necessary to get that key name to be able to get the Array from that keys value.
That is what I do by derfeferencing with the key name at index 1 returned by StructKeyArray() method:
<cfset trends=current.trends[trendsName[1]] >

That does the trick.


0
 

Author Comment

by:RedBullLee
ID: 33491115
Thank you, Zvonko - that has worked perfectly!

I had reverted to using an XML feed last night, but having come back to this following your reply, the Json method is significantly faster.

I appreciate the explanation too - I can see where I was going wrong!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 33491905
You are welcome.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Cross contaminating files and websites on same network. 7 57
Controlling printer trays from browser 3 139
Database set up 5 60
paging 3 28
The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now