2012-05-03 23 views
1

我試圖找回從cfc類型的列表,並將其作爲JSON返回。問題是如何創建JSON的結構。我一直在絞盡腦汁,試圖以json格式來獲取它,而且我不確定它是否可以像目前所寫的那樣。多個表相同的列數據結構CFC 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

你試過只是'serializeJSON(getAllTypes,真)'? – Henry

回答

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)創建一個結構來保存數據並使用CFOUTPUT與GROUP屬性填充它:

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

心連心

相關問題