Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

json - How to return in struct with my cfoutput?

I am trying to output certain key values to show(WHICH I AM GETTING ALREADY). But for some reason it is returning as a struct. Basically, I am converting from query statement into api call. But for some reason I cant get it return from cfoutput to show as a struct from the api call or a json file. Can anyone tell me what I have done wrong. thanks for the help. here is my code:

Output: I get all my json fields showing in a struct. (with cfscript uncommented out)

Update output with current code: (I get my output of certain key value, but can I do iterate through a loop, I know the way I have it is not the best way)

<cfset?jsonDatas?=?fileRead("c:UsersDesktopmyApi.json"?)>?????
<cfset?jsonData?=?deserializeJSON(jsonDatas)?/>??????
<cfdump?var="#jsonData#"?abort="true">??


   <cfloop?array="#jsonData#"?index="prop">??
    <cfoutput>
    <br>Output:?
    #prop.employeeId#
    .....

    </cfoutput>
    ?</cfloop>? 

My Json:

    [?
{?
?"employeeId"?:?"77777",??
"lastName"?:?"DOE",??
"firstName"?:?"JOHN",??
"middleName"?:?null,??
"sex"?:?"Male",??
"jobStatus"?:?"Active",?
"jobStatusDate"?:?"2020-01 03?00:00:00.0",??
"departmentNbr"?:?"5555",
}
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's see if the third try does the trick.

I added more data so that we can see that each array is processed separately.

<cfscript>
    json =

     '[ 
{ 
 "employeeId" : "77777",  
"lastName" : "DOE",  
"firstName" : "JOHN",  
"middleName" : null,  
"sex" : "Male",  
"jobStatus" : "Active", 
"jobStatusDate" : "2020-01 03 00:00:00.0",  
"departmentNbr" : "5555"
},
{ 
 "employeeId" : "123",  
"lastName" : "Doe",  
"firstName" : "Jane",  
"middleName" : "H",  
"sex" : "Female",  
"jobStatus" : "Active", 
"jobStatusDate" : "2021-01 03 00:00:00.0",  
"departmentNbr" : "14"
}
]';

jsonData = DeserializeJSON(json);

writedump(jsonData);

for (row in jsonData)   {
    writeoutput("<hr />");
    for (item in row)   {
        writeoutput(item & ": " & row[item] & "<br />");
    }
}

</cfscript>

See: https://cffiddle.org/app/file?filepath=ca5836f8-af85-4878-a2d7-9406bdd4a884/dc41f7b8-3792-4141-bd0b-7a9839900a43/97dfe9de-53f2-4e82-8e8c-53fd5b5bfd36.cfm

Or From a file

<cfscript>

jsonDatas = fileRead("c:UsersDesktopmyApi.json" );  
jsonData = deserializeJSON(jsonDatas);

writedump(jsonData);

for (row in jsonData)   {
    writeoutput("<hr />");
    for (item in row)   {
        writeoutput(item & ": " & row[item] & "<br />");
    }
}

</cfscript>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...