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
Solved

cfloop through json

Posted on 2012-12-21
2
1,368 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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