如何以JSON文件格式显示列名称? (通过ColdFusion) [英] How can I show the column names in my JSON file format? (Through ColdFusion)

查看:342
本文介绍了如何以JSON文件格式显示列名称? (通过ColdFusion)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过下面的ColdFusion .cfm文件,我能够得到一个.json字符串,它显示了所请求的数据:

Through the following ColdFusion .cfm file, I am able to get a .json string, which shows me the requested data:

json1.cfm

<cfsetting showdebugoutput="no">
<cfheader name="Content-Type" value="application/json">
<cfquery name="GetData" datasource="myDataSource">
    select distinct ap1, ap2, nombre, idcargo, idencal, telefono, email, cve_nivel, sexo
    FROM vlayout_1
    where cct='13DCC0003S'
</cfquery>

<cfoutput>
#SerializeJSON(GetData, false)#
</cfoutput>

一旦我在我的本地服务器上上传,这是结果.json字符串:

Once I upload it on my local server, this is the outcome .json string:

{"COLUMNS":["AP1","AP2","NOMBRE","IDCARGO","IDENCAL","TELEFONO","EMAIL","CVE_NIVEL","SEXO"],"DATA":[["ALVARADO","HERNANDEZ","ALEJANDRO",3,1,"","",5,"M"],["BAUTISTA","OSORIO","ANTONIO",3,5,"","",6,"M"],["HERNANDEZ","ALVARADO","LAURA",3,5,"","",6,"F"],["HERNANDEZ","ANDRADE","MA. TERESA",2,5,"","",6,"F"],["HERNANDEZ","HERNANDEZ","FILOMENA",3,5,"","",4,""],["HERNANDEZ","HERNANDEZ","FILOMENA",3,5,"","",5,""],["HERNANDEZ","HERNANDEZ","MARIA GUADALUPE",3,5,"","",5,"F"],["HERNANDEZ","HERNANDEZ","MARIA LUISA",3,5,"","",4,"F"],["HERNANDEZ","MARTINEZ","MARIA MANUELA",3,5,"","",4,"F"],["HERNANDEZ","QUINTERO","CIRILA",3,5,"","",5,"F"],["LORENZO","LEON","JUAN",3,5,"","",6,"M"],["MARTINEZ","HERNANDEZ","ROSALBA",1,5,"","",5,"F"],["SIXTO","RAMIREZ","EUTIQUIO",3,5,"","",4,"M"],["SIXTO","RAMIREZ","EUTIQUIO",3,5,"","",5,"M"]]} 

http:// jsonlint上验证后.com / 这是我得到的:

{
    "COLUMNS": [
        "AP1",
        "AP2",
        "NOMBRE",
        "IDCARGO",
        "IDENCAL",
        "TELEFONO",
        "EMAIL",
        "CVE_NIVEL",
        "SEXO"
    ],
    "DATA": [
        [
            "ALVARADO",
            "HERNANDEZ",
            "ALEJANDRO",
            3,
            1,
            "",
            "",
            5,
            "M"
        ],
        [
            "BAUTISTA",
            "OSORIO",
            "ANTONIO",
            3,
            5,
            "",
            "",
            6,
            "M"
        ],
        [
            "HERNANDEZ",
            "ALVARADO",
            "LAURA",
            3,
            5,
            "",
            "",
            6,
            "F"
        ],
        [
            "HERNANDEZ",
            "ANDRADE",
            "MA. TERESA",
            2,
            5,
            "",
            "",
            6,
            "F"
        ],
        [
            "HERNANDEZ",
            "HERNANDEZ",
            "FILOMENA",
            3,
            5,
            "",
            "",
            4,
            ""
        ],
        [
            "HERNANDEZ",
            "HERNANDEZ",
            "FILOMENA",
            3,
            5,
            "",
            "",
            5,
            ""
        ],
        [
            "HERNANDEZ",
            "HERNANDEZ",
            "MARIA GUADALUPE",
            3,
            5,
            "",
            "",
            5,
            "F"
        ],
        [
            "HERNANDEZ",
            "HERNANDEZ",
            "MARIA LUISA",
            3,
            5,
            "",
            "",
            4,
            "F"
        ],
        [
            "HERNANDEZ",
            "MARTINEZ",
            "MARIA MANUELA",
            3,
            5,
            "",
            "",
            4,
            "F"
        ],
        [
            "HERNANDEZ",
            "QUINTERO",
            "CIRILA",
            3,
            5,
            "",
            "",
            5,
            "F"
        ],
        [
            "LORENZO",
            "LEON",
            "JUAN",
            3,
            5,
            "",
            "",
            6,
            "M"
        ],
        [
            "MARTINEZ",
            "HERNANDEZ",
            "ROSALBA",
            1,
            5,
            "",
            "",
            5,
            "F"
        ],
        [
            "SIXTO",
            "RAMIREZ",
            "EUTIQUIO",
            3,
            5,
            "",
            "",
            4,
            "M"
        ],
        [
            "SIXTO",
            "RAMIREZ",
            "EUTIQUIO",
            3,
            5,
            "",
            "",
            5,
            "M"
        ]
    ]
}

问题是,如何在内容之前显示每个列名称?例如:

The question is, how can I show every column name before its content? e.g.:

 [
                AP1:"SIXTO",
                AP2:"RAMIREZ",
                NOMBRE:"EUTIQUIO",
                IDCARGO:3,
                IDENCAL:5,
                TELEFONO:"",
                EMAIL:"",
                CVE_NIVEL:5,
                SEXO:"M"
            ]

/ p>

Thank you so much in advance!

推荐答案

您需要将其转换为结构数组,然后应用 serializeJSON code>。

You'll need to convert it to an array of structures, then apply serializeJSON().

<cfsetting showdebugoutput="no">
<cfheader name="Content-Type" value="application/json">
<cfquery name="GetData" datasource="myDataSource">
    select distinct ap1, ap2, nombre, idcargo, idencal, telefono, email, cve_nivel, sexo
    FROM vlayout_1
    where cct='13DCC0003S'
</cfquery>

<cfoutput>
#SerializeJSON(queryToArray(GetData))#
</cfoutput>


<cfscript>
/**
 * @hint Converts a query to an array of structures.
 */
public array function queryToArray(required query query){
    var result = [];
    for (var i = 1; i <= query.recordCount; i++){
        arrayAppend(result, queryToStruct(query, i));
    }
    return result;
}

/**
 * @hint Returns the first row of a query as a structure with same case as query column names.
 */
public struct function queryToStruct(required query query, numeric rowNumber=1){

    var struct = {};
    var columns = arguments.query.getMeta().getColumnLabels();

    for (var i = 1; i <= arrayLen(columns); i++){
        struct[columns[i]] = query[columns[i]][arguments.rowNumber];
    }

    return struct;
}
</cfscript>

这篇关于如何以JSON文件格式显示列名称? (通过ColdFusion)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆