Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Coldfusion - Json Data Output

Posted on 2010-08-20
4
Medium Priority
?
749 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 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

648 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