Solved

Coldfusion - Json Data Output

Posted on 2010-08-20
4
723 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
https json cfhttp post 1 119
multi page pdf 4 89
what type of join should I use on these 2 queries 9 99
Test if a nested structure variable exisits : Coldfusion 8 10 40
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…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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