问题
If I do the following on a query :
<cfdump var="#serializeJSON(findglobal)#">
I get the following:
{
"COLUMNS": [
"DELIVERED_PERCENTAGE",
"UNIQUE_PERCENTAGE",
"SPAM_PERCENTAGE",
"DROP_PERCENTAGE",
"REQUEST_PERCENTAGE",
"BOUNCE_PERCENTAGE",
"DEFERRED_PERCENTAGE",
"PROCESSED_PERCENTAGE",
"OPEN_PERCENTAGE",
"BLOCKED_PERCENTAGE"
],
"DATA": [
[
19.54,
6.06,
6.05,
0.63,
21.17,
0.85,
14.83,
20.53,
10.26,
0.19
]
]
}
But I am using Geikoboard which understand only the following format of JSON.
So I would like to have DELIVERED_PERCENTAGE, UNIQUE_PERCENTAGE for the label field below and all the values, like
19.54,6.06 etc for the value field below.
{
"item": [
{
"value": "11234",
"label": "Webmail",
"colour": "FFFF10AA"
},
{
"value": "10736",
"label": "Phone",
"colour": "FFAA0AAA"
},
{
"value": "230",
"label": "Webmail",
"colour": "FF5505AA"
},
{
"value": "280",
"label": "Webmail",
"colour": "FF0000AA"
}
]
}
Do I have to manually generate JSON ?
回答1:
I think this is what you are looking for. Got it from some site; either Raymon Camden's or Ben Nadel's.
public array function queryToArray( required query qry ) {
var columns = arguments.qry.getColumnNames();
var OutputResult = [];
for( var i = 1; i LTE qry.recordCount; i++ ) {
var obj = {};
for( var k = 1; k LTE arrayLen( columns ); k++ ) {
structInsert( obj, columns[ k ], arguments.qry[ columns[ k ] ][ i ] );
}
arrayAppend(OutputResult, obj );
}
return OutputResult;
}
You would need to do something like this:
<cfset myJSON = queryToArray( myquery ) />
<cfoutput>#serializeJSON( myJSON )#</cfoutput>
来源:https://stackoverflow.com/questions/22950028/putting-selected-values-from-the-json