Home > OS >  Looping a nesty struture in Coldfusion
Looping a nesty struture in Coldfusion

Time:12-29

Here is my JSON Data: This i am trying to loop and get all the information about every node of it with the key name and the key data

{
    "src": 1,
    "feeds": [{
        "id": 1,
        "name": "All",
        "polarPlot": true
    }],
    "srcFeed": 1,
    "showSil": true,
    "showFlg": true,
    "showPic": true,
    "flgH": 20,
    "flgW": 85,
    "acList": [{
        "Id": 12589424,
        "Rcvr": 1,
        "HasSig": false,
        "Icao": "C01970",
        "Bad": false,
        "Reg": "C-FJQL",
        "FSeen": "\/Date(1672216397120)\/",
        "TSecs": 334,
        "CMsgs": 330,
        "Alt": 34975,
        "GAlt": 35206,
        "InHg": 30.15059,
        "AltT": 0,
        "Call": "ROU1958",
        "Lat": 45.59019,
        "Long": -74.90746,
        "PosTime": 1672216731130,
        "Mlat": false,
        "Tisb": false,
        "Spd": 469.0,
        "Trak": 59.3,
        "TrkH": false,
        "Type": "A321",
        "Mdl": "Airbus A321 211SL",
        "Man": "Airbus",
        "CNum": "7117",
        "From": "YYZ Lester B. Pearson, Toronto, Canada",
        "To": "YQB Quebec Jean Lesage, Canada",
        "Op": "Air Canada Rouge",
        "OpIcao": "ROU",
        "Sqk": "2222",
        "Help": false,
        "Vsi": 0,
        "VsiT": 0,
        "WTC": 2,
        "Species": 1,
        "Engines": "2",
        "EngType": 3,
        "EngMount": 0,
        "Mil": false,
        "Cou": "Canada",
        "HasPic": false,
        "Interested": false,
        "FlightsCount": 2,
        "Gnd": false,
        "SpdTyp": 0,
        "CallSus": false,
        "Trt": 2,
        "Year": "2016"
    }, {
        "Id": 12595324,
        "Rcvr": 1,
        "HasSig": false,
        "Icao": "C0307C",
        "Bad": false,
        "Reg": "C-FSJJ",
        "FSeen": "\/Date(1672215986531)\/",
        "TSecs": 745,
        "CMsgs": 284,
        "Alt": 35000,
        "GAlt": 35189,
        "InHg": 30.1092529,
        "AltT": 0,
        "Call": "ACA394",
        "Lat": 47.86931,
        "Long": -72.90211,
        "PosTime": 1672216731064,
        "Mlat": false,
        "Tisb": false,
        "Spd": 513.0,
        "Trak": 117.2,
        "TrkH": false,
        "Type": "B38M",
        "Mdl": "Boeing 737MAX 8",
        "Man": "Boeing",
        "CNum": "61217",
        "From": "YYC Calgary, Canada",
        "To": "YHZ Halifax / Stanfield, Canada",
        "Op": "Air Canada",
        "OpIcao": "ACA",
        "Sqk": "1144",
        "Help": false,
        "Vsi": 0,
        "VsiT": 0,
        "WTC": 2,
        "Species": 1,
        "Engines": "2",
        "EngType": 3,
        "EngMount": 0,
        "Mil": false,
        "Cou": "Canada",
        "HasPic": false,
        "Interested": false,
        "FlightsCount": 2,
        "Gnd": false,
        "SpdTyp": 0,
        "CallSus": false,
        "Trt": 2,
        "Year": "2018"
    }, {
        "Id": 12601386,
        "Rcvr": 1,
        "HasSig": false,
        "Icao": "C0482A",
        "Bad": false,
        "Reg": "C-GBIN",
        "FSeen": "\/Date(1672215747131)\/",
        "TSecs": 984,
        "CMsgs": 5124,
        "Alt": 32925,
        "GAlt": 33185,
        "InHg": 30.1801186,
        "AltT": 0,
        "Call": "ROU1689",
        "Lat": 45.119546,
        "Long": -71.834575,
        "PosTime": 1672216731191,
        "Mlat": false,
        "Tisb": false,
        "Spd": 462.5,
        "Trak": 31.1,
        "TrkH": false,
        "Type": "A319",
        "Mdl": "Airbus A319 114",
        "Man": "Airbus",
        "CNum": "845",
        "From": "FLL Fort Lauderdale Hollywood, United States",
        "To": "YQB Quebec Jean Lesage, Canada",
        "Op": "Air Canada Rouge",
        "OpIcao": "ROU",
        "Sqk": "1447",
        "Help": false,
        "Vsi": -2112,
        "VsiT": 0,
        "WTC": 2,
        "Species": 1,
        "Engines": "2",
        "EngType": 3,
        "EngMount": 0,
        "Mil": false,
        "Cou": "Canada",
        "HasPic": false,
        "Interested": false,
        "FlightsCount": 1,
        "Gnd": false,
        "SpdTyp": 0,
        "CallSus": false,
        "Trt": 2,
        "Year": "1998"
    }, {
        "Id": 10812570,
        "Rcvr": 1,
        "HasSig": false,
        "Icao": "A4FC9A",
        "Bad": false,
        "Reg": "N420LA",
        "FSeen": "\/Date(1672215081608)\/",
        "TSecs": 1650,
        "CMsgs": 4226,
        "Alt": 34000,
        "GAlt": 34281,
        "InHg": 30.2007866,
        "AltT": 0,
        "Call": "LAE2512",
        "Lat": 46.00371,
        "Long": -71.20912,
        "PosTime": 1672216731894,
        "Mlat": false,
        "Tisb": false,
        "Spd": 399.0,
        "Trak": 210.7,
        "TrkH": false,
        "Type": "B763",
        "Mdl": "Boeing 767 316F/W",
        "Man": "Boeing",
        "CNum": "34627",
        "Op": "MAS Air Cargo",
        "OpIcao": "MAA",
        "Sqk": "0706",
        "Help": false,
        "Vsi": 0,
        "VsiT": 0,
        "WTC": 3,
        "Species": 1,
        "Engines": "2",
        "EngType": 3,
        "EngMount": 0,
        "Mil": false,
        "Cou": "United States",
        "HasPic": false,
        "Interested": false,
        "FlightsCount": 1,
        "Gnd": false,
        "SpdTyp": 0,
        "CallSus": false,
        "Trt": 2,
        "Year": "2006"
    }, {
        "Id": 12706028,
        "Rcvr": 1,
        "HasSig": false,
        "Icao": "C1E0EC",
        "Bad": false,
        "FSeen": "\/Date(1672196473179)\/",
        "TSecs": 20258,
        "CMsgs": 1605,
        "Alt": 125300,
        "GAlt": 125300,
        "AltT": 0,
        "Call": "TEST1234",
        "Tisb": false,
        "TrkH": false,
        "Sqk": "",
        "VsiT": 0,
        "WTC": 0,
        "Species": 0,
        "EngType": 0,
        "EngMount": 0,
        "Mil": false,
        "Cou": "Canada",
        "HasPic": false,
        "Interested": false,
        "FlightsCount": 4,
        "Gnd": false,
        "SpdTyp": 0,
        "CallSus": false,
        "Trt": 1
    }],
    "totalAc": 5,
    "lastDv": "638077811342841157",
    "shtTrlSec": 30,
    "stm": 1672216731909
}   

I am trying to loop and have the data for every loop in the nesty structure As depending on the plane info, data is often changed fopr more or less inside nesty loop I have tried everything on my low knowledge of coldfusion Need some help and many thanks to all

CodePudding user response:

Save it as a variable, then deserialize.

<cfset JSONObject = DeserializeJSON(YourVariable)>

You can then call specific key-values from it like #JSONObject.flgH#.

If you have multiple to loop through you can output it with array notation, like #JSONObject.acList.Reg[1]# - you would replace the number with a variable like [i] if you're looping through it dynamically.

CodePudding user response:

You can start with:

<cfset local.stData = deserializeJSON(whatever that variable is holding the JSON) />
<!--- Now, local.stData.acList is what is called "an array of structs" --->
<cfloop array="#local.stData.acList#" index="local.n" item="local.stPlane">
    <!--- In here, you can work with local.stPlane.id, for example --->
    <!--- If you need to loop over the "keys" of the struct, like "Id", "Rcvr", you can do that, too. I prefer structKeyList --->
    <cfloop list="#structKeyList(local.stPlane)#" index="local.sKey">
        <cfif local.sKey = 'Id'>
        <cfelseif local.sKey = 'Rcvr'>
        </cfif>
        <!--- Or... --->
        <cfswitch expression="#local.sKey#">
            <cfcase value="Id">
                
            </cfcase>
            <cfcase value="Rcvr">

            </cfcase>
            <cfdefaultcase>
                <!--- For unexpected keys, or just skip this block --->
            </cfdefaultcase>
        </cfswitch>
    </cfloop>
</cfloop>
  • Related