Solved

cfloop through json

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

758 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

17 Experts available now in Live!

Get 1:1 Help Now