1

我正在尝试从 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',...}}]

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

有什么想法吗?

4

1 回答 1

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

于 2012-05-04T01:12:48.040 回答