多个表相同的列数据结构CFC JSON

时间:2012-05-03 19:44:53

标签: json coldfusion cfc

我试图从cfc返回一个类型列表,并将其作为JSON返回。问题是如何创建JSON的结构。我一直绞尽脑汁,试图以json格式制作它,我不确定它是否可能像目前所写的那样。

所以这是设置。我有4个表,除了一列之外都是相同的。我需要每个表中有多行。

我尝试从每个表中获取相应的数据,如下所示:

<cfscript>
    tempStruct = setAttributionTypes(dsn,type);
    tempStruct = setCharacteristicTypes(dsn,type);
    //tempArray = setExposureTypes(dsn,type);
    //tempArray = setWeightTypes(dsn,type);             
</cfscript>

正如你所看到的,我正在尝试不同的方法。创建结构,并创建结构数组(未显示)

以下是我用来将所有字段放回一列的当前查询:

<cfquery name="getAllTypes" datasource="#dsn#">
    SELECT udc_code,
           type
    FROM(
        SELECT attribution_id AS udc_code,type
        FROM   tbl_asset_profile_template_attributions
        WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">
        UNION ALL
        SELECT characteristic_id AS udc_code,type
        FROM   tbl_asset_profile_template_characteristics
        WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">
        UNION ALL
        SELECT exposure_id AS udc_code,type
            FROM   tbl_asset_profile_template_exposures
        WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">
        UNION ALL
        SELECT weight_id AS udc_code,type
        FROM   tbl_asset_profile_template_weights
        WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">) AS tbl
    GROUP  BY type,
                 udc_code
    ORDER  BY type 
</cfquery>

我创建了一个类似[{'attribution1':data,...}{...}]的结构,但这很难处理。

我一直在尝试创建这样的结构:

 [{ATTRIBTUIONS{'TYPE1','TYPE2',}},{CHARACTERISTICS{'TYPE1',...}}]

我只是很难想象我需要什么。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

通过一些查询调整和数据按摩,您可以获得所需的json对象:

1)将您的查询更改为:

<cfquery name="getAllTypes" datasource="#dsn#">
SELECT profilename,udc_code
FROM (
    SELECT attribution_id AS udc_code, type, 'Attributions' AS profilename
    FROM   tbl_asset_profile_template_attributions
    WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">
    UNION ALL
    SELECT characteristic_id AS udc_code, type, 'Characteristics' AS profilename
    FROM   tbl_asset_profile_template_characteristics
    WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">
    UNION ALL
    SELECT exposure_id AS udc_code, type, 'Exposures' AS profilename
    FROM   tbl_asset_profile_template_exposures
    WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">
    UNION ALL
    SELECT weight_id AS udc_code, type, 'Weights' AS profilename
    FROM   tbl_asset_profile_template_weights
    WHERE  template_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#type#">) AS tbl
GROUP  BY profilename, type, udc_code
ORDER  BY profilename, type 
</cfquery>

2)创建一个结构来保存您的数据并使用带有GROUP属性的cfoutput填充它:

<cfset stTypes = structnew()>

<cfoutput query="getAllTypes" group="profilename">
 <cfset stTypes[getAllTypes.profilename] = arraynew(1)>
 <cfoutput>
  <cfset arrayappend(stTypes[getAllTypes.profilename], getAllTypes.type)>
 </cfoutput>
</cfoutput>

3)将结构序列化为json对象:

<cfset jsonobj = serializejson(stTypes)>

这将创建一个像这样的json对象:

{'Attributions':['Type1', 'Type2',...], 'Characteristics':['TypeA', 'TypeB',...], ...}

HTH