Solved

Coldfusion - Json Data Output

Posted on 2010-08-20
4
727 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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