putting selected values from the JSON

≡放荡痞女 提交于 2019-12-11 06:57:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!