Thread: JSON structure

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    151
    Rep Power
    19

    JSON structure


    ObjGen - Live JSON Generator

    Happy Friday, it's finally here. My problem for the week that i can't seem to figure out is this...

    I'm learning AngularJS, specifically nested ng-repeats. I'm passing some JSON created from a CFC method to my controller. The JSON structure i'm trying to create is:
    ObjGen - Live JSON Generator
    Code:
    {
      "Heading1": [
        "name1",
        "name2",
        "name3"
      ],
      "Heading2": [
        "name1",
        "name2",
        "name3"
      ],
      "Heading3": [
        "name1",
        "name2",
        "name3"
      ]
    }
    The goal is to display the names under each heading.

    My problem is this and i don't know if i'm approaching this right. I'm trying to generate the JSON from arrays and structures that get run through serializeJSON() in ColdFusion. My results are so far off though i'm wondering if this is the right way to do it or if i just really need practice with structures and arrays.

    The database structure is a very simple relational setup using two tables Famous_Catagories, Famous_People.

    At the very least i'm hoping to find out if i need to put an array in a structure or use some other method.

    Thanks,
    DSFX.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    151
    Rep Power
    19
    Took a step away form it today to get my head together and i think i've figured it out.

    Code:
     <!--- Setup my variables for the indexing/struct/arrays --->
            <cfset myData = [] />
            <cfset myCatagory = structNew() />
            <cfset myNames = [] />
            <cfset Index = 0 />
            
    
            <cfquery name="getCatagory" datasource="external">
                SELECT catagory
                FROM famous_catagories
            </cfquery>
    
    
            <cfoutput>
                <!--- loop through the catagories --->
                <cfloop query="getCatagory">
                    <cfset nameIndex = 0 />
                    <!--- Now clear the array. --->
                    <cfset Temp = ArrayClear(myNames)>
                    <!---get names in each catagory --->
                    <cfquery name="getNames" datasource="external">
                        SELECT name
                        FROM famous_guelphites
                        WHERE catagory = '#getCatagory.catagory#'
                    </cfquery>
    
                    <!---Build the name array --->
                    <cfloop query="getNames">
                        <cfset nameIndex = #nameIndex# + 1 />
                        <cfset myNames[#nameIndex#] = "#getNames.name#" />
                    </cfloop>
    
                    <!--- save all data to the structure that will hold the catagory and peoples names in athat catagory --->
                    <cfset Index = #Index# + 1 />
                    <cfset myCatagory.Catagory = #getCatagory.catagory# />
                    <cfset myCatagory.Names = #myNames# />
    
                    <!--- copy the structure to the master array that will hold all the catagory structures --->
                    <cfset myData[#Index#] = #structCopy(myCatagory)# />
    
                </cfloop>
            </cfoutput>
    
    
            <cfreturn serializeJSON(myData, true)>
    Another one bites the dust.

    DSFX
    Edit found a bug
    Last edited by dsfx; November 21st, 2015 at 04:10 PM.

IMN logo majestic logo threadwatch logo seochat tools logo