Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

cfloop through json

Posted on 2012-12-21
2
Medium Priority
?
1,697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 1000 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 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
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 …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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