Solved

DeserializeJSON() Cold Fusion - Facebook Photo Album Feed

Posted on 2012-04-09
9
1,385 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now