Solved

cfloop through json

Posted on 2012-12-21
2
1,320 Views
Last Modified: 2013-01-08
Hello,

I'm trying to loop through this json link...
<!--- Get the JSON Feed --->
<cfhttp url="http://www.geoplugin.net/extras/nearby.gp?limit=50&radius=50&format=json">

<!--- 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['geoplugin_place']#" index="struct2">
                        
                  </cfloop>
            </cfloop>
            </cfoutput>
</cfif> 

Open in new window



any help would be great!

Thanks
0
Comment
Question by:brihol44
2 Comments
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 250 total points
ID: 38715001
without having any idea what the data looks like, this is based on what it appears you're trying to do.   First, you are looping a structure and the structure is made up of arrays... ?


<cfloop collection="#cfData#" item="key">
   <!--- the result of this would be  cfData[key],  
         IF this is an array, then you would loop it like this: ---->
   <cfloop index="pos" from="1" to="#arrayLen(cfData[key])#">
        #cfData[Key][pos]#
   </cfloop>
</cfloop>
0
 
LVL 2

Accepted Solution

by:
sumCold earned 250 total points
ID: 38721025
hi,

I modified your code, check below:

<!--- Get the JSON Feed --->
<cfhttp url="http://www.geoplugin.net/extras/nearby.gp?limit=50&radius=50&format=json">

<!--- 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)>
    <!--- <cfdump var="#cfData#"> --->
    <!--- 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 array="#cfData#" index="i"> <!--- Loop for array --->
                <cfloop collection="#i#" item="k"> <!--- loop for structure inside array --->
                   #i[k]#
                </cfloop>  
                <br/><br/>
              </cfloop>  
            <!--- <cfloop collection="#cfData#" item="key">
                  <cfloop array="#cfData['geoplugin_place']#" index="struct2">
                       
                  </cfloop>
            </cfloop> --->
            </cfoutput>
</cfif>
0

Featured Post

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.

Question has a verified solution.

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

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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