本文介绍了从JSON中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我对查询执行以下操作:

If I do the following on a query :

<cfdump var="#serializeJSON(findglobal)#">

我得到以下结果:

{
    "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
        ]
    ]
}

但我使用,它只能理解JSON的以下格式。
所以我想为下面的标签字段和所有的值, DELIVERED_PERCENTAGE UNIQUE_PERCENTAGE
19.54,6.06等为下面的值字段。

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, like19.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"
}
]
}


b $ b

我必须手动生成JSON吗?

Do I have to manually generate JSON ?

推荐答案

我认为这是你正在寻找的。从某些网站得到它; Raymon Camden的或Ben Nadel的。

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;
}

您需要这样做:

<cfset myJSON = queryToArray( myquery ) />
<cfoutput>#serializeJSON( myJSON )#</cfoutput>

这篇关于从JSON中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 07:28