?
Solved

Coldfusion - Json Data Output

Posted on 2010-08-20
4
Medium Priority
?
754 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 2000 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

809 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