Solved

DeserializeJSON() Cold Fusion - Facebook Photo Album Feed

Posted on 2012-04-09
9
1,388 Views
Last Modified: 2012-04-11
I have the following snippet of code from the Adobe website to try and bring the JSON data into my site, but I have no idea how to parse or read the data after I retrieve it.  My goal is to simply create a viewable photo album on my site but using the photos stored on Facebook. I can get the photos using the CFHTTP line in my code, but don't know how to read that data into a usable format for creating an album.

I think it involves a CFLOOP for dumping the data out, but I only need the actual URL's of the photos themselves, not all the other data in the JSON output.

This is the code I'm using so far...

<!--- Get the JSON Feed --->
<cfhttp url="https://graph.facebook.com/265379466888505/photos">
 
<!--- JSON data is sometimes distributed as a JavaScript function.
     The following REReplace functions strip the function wrapper. --->
<cfset theData=REReplace(cfhttp.FileContent,  
        "^\s*[[:word:]]*\s*\(\s*","")>
<cfset theData=REReplace(theData, "\s*\)\s*$", "")>
 
<!--- Test to make sure you have JSON data. --->
<cfif !IsJSON(theData)>
    <h3>The URL you requested does not provide valid JSON</h3>
    <cfdump var="#theData#">
 
<!--- If the data is in JSON format, deserialize it. --->
<cfelse>
    <cfset cfData=DeserializeJSON(theData)>
0
Comment
Question by:day6
  • 4
  • 3
  • 2
9 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37823703
You can easily deserialise within the client, as in this jsfiddle.
0
 
LVL 1

Author Comment

by:day6
ID: 37823817
Proculopsis:  I need a Cold Fusion script which outputs the URL's, not a piece of middleware I have to manually plug in the code.  I want to host the CF Script on my website and create a photo album viewer based on the auto-feeds from the JSON script. I don't use PHP on my server.
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37823842
Then you'll need to cfloop cfData in a similar way to what I've done in the jsfiddle.
0
 
LVL 1

Author Comment

by:day6
ID: 37823976
yea, I posted that in my original question. I know i need to CFLOOP the output. But I obviously don't know the way to write that code... which is why I posted this question to begin with. I don't know how to read PHP, so your suggestion of writing it in CF from your PHP example doesn't help me.

A CF expert would probably know how to output the data from the CF tag that is reading the JSON. I know you don't program in CF, so hopefully someone who is a CF pro can read this question and help me out.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 2

Expert Comment

by:sumCold
ID: 37826928
0
 
LVL 1

Author Comment

by:day6
ID: 37827125
sumcold: that's where the code i posted came from
0
 
LVL 2

Accepted Solution

by:
sumCold earned 500 total points
ID: 37828890
Try below code..hope it helps: :)

<!--- Get the JSON Feed --->
<cfhttp url="https://graph.facebook.com/265379466888505/photos">

<!--- JSON data is sometimes distributed as a JavaScript function.
     The following REReplace functions strip the function wrapper. --->
<cfset theData=REReplace(cfhttp.FileContent,
        "^\s*[[:word:]]*\s*\(\s*","")>
<cfset theData=REReplace(theData, "\s*\)\s*$", "")>

<!--- Test to make sure you have JSON data. --->
<cfif !IsJSON(theData)>
    <h3>The URL you requested does not provide valid JSON</h3>
    <cfdump var="#theData#">

<!--- If the data is in JSON format, deserialize it. --->
<cfelse>
    <cfset cfData=DeserializeJSON(theData)>
    <!--- Parse the resulting array or structure and display the data.
             In this case, the data represents a ColdFusion query that has been
             serialized by the SerializeJSON function into a JSON structure with
             two arrays: an array column names, and an array of arrays,
             where the outer array rows correspond to the query rows, and the
             inner array entries correspond to the column fields in the row. --->
    <!--- First, find the positions of the columns in the data array. --->
   
            <cfoutput>
            <cfloop collection="#cfData#" item="key">
                  <cfloop array="#cfData['data']#" index="struct2">
                        <cfloop collection="#struct2#" item="key2">
                              <cfloop array="#struct2['images']#" index="struct3">
                                    <img src="#struct3['source']#" height="#struct3['height']#" width="#struct3['width']#">
                              </cfloop>
                        </cfloop>
                  </cfloop>
            </cfloop>
            </cfoutput>
</cfif>
0
 
LVL 1

Author Comment

by:day6
ID: 37829282
I'm using this code to isolate just ONE URL per array item.

<cfoutput>
            <cfloop collection="#cfData#" item="key">
                  <cfloop array="#cfData['data']#" index="struct2">
                        <cfloop collection="#struct2#" item="key2">
                              <cfloop array="#struct2['images']#" index="struct3">
                                    <cfif struct3['height'] is 480>
                                                      #struct3['source']#<hr />
                                                      </cfif>
                              </cfloop>
                        </cfloop>
                  </cfloop>
            </cfloop>
            </cfoutput>

However, my result set has 8 duplicates of each single URL from each set. So it outputs 8 duplicates of each.
0
 
LVL 2

Expert Comment

by:sumCold
ID: 37831378
Its nice to hear your problem is solved and you understand many things..
thanks for points :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now