Skip to main content
lovewebdev
Inspiring
June 11, 2011
Question

How to display data from JSON

  • June 11, 2011
  • 1 reply
  • 2461 views

Guys Im banging my head against the wall.

So Ive got JSON being returned from a cfhttp.

How the heck do I make use of that data. Everything I do gives me you get turn a complex object into a simple object.

My last line is

<cfdump var="#friendsl#"> which has this JSON from Facebook:

{
   "data": [
      {
         "name": "whatever whatever",
         "id": "554545453"
      }
   ]
}

I've tried looping over it as a structure, nothing. Ive tried turning it into a query, nothing.

Any help would be much appreciated.

This topic has been closed for replies.

1 reply

Inspiring
June 11, 2011

<cfdump var="#friendsl#"> which has this JSON

That is just a string. If you want to convert it into CF arrays/structures you need to deserialize it. See the docs on deserializeJSON

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_s_03.html

lovewebdev
Inspiring
June 11, 2011

I left that out in my earlier post accidentally. I did deserialize, but can get it to work.

<cfset friendsl=DeserializeJSON(friends.filecontent)>

    <cfdump var="#friendsl#">

I've tried

<cfset colList=ArrayToList(friendsl.data)>

    <cfset colid=ListFind(colList, "id")>

    <cfset colname=ListFind(colList, "name")>

        <cfloop index="i" from="1" to="#ArrayLen(friendsl.DATA)#">

            <h3>#friendsl.DATA[colid]#</h3>

        </cfloop>

I get

Complex object types cannot be converted to simple

Inspiring
June 11, 2011

Sorry, nothing beyond the cfdump showed up in my email.   I really HATE these forums...

<cfset colList=ArrayToList(friendsl.data)>

Now that I can actually +see+ your code ... the problem is the array elements are not simple strings. Each element is a structure (keys "id" and "name"). So that is why the ArrayToList conversion fails.  But you do not need it anyway. Simply loop through the array and access the "id" and "name" keys directly

     <cfloop index="i" from="1" to="#ArrayLen(friendsl.DATA)#">

                #friendsl.DATA.id# .... #friendsl.DATA.name#

      </cfloop>

Message was edited by: -==cfSearching==-