• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1489
  • Last Modified:

DeserializeJSON() Cold Fusion - Facebook Photo Album Feed

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
day6
Asked:
day6
  • 4
  • 3
  • 2
1 Solution
 
ProculopsisCommented:
You can easily deserialise within the client, as in this jsfiddle.
0
 
day6Author Commented:
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
 
ProculopsisCommented:
Then you'll need to cfloop cfData in a similar way to what I've done in the jsfiddle.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
day6Author Commented:
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
 
day6Author Commented:
sumcold: that's where the code i posted came from
0
 
sumColdCommented:
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
 
day6Author Commented:
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
 
sumColdCommented:
Its nice to hear your problem is solved and you understand many things..
thanks for points :)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now