使用coldfusion解析字符串

时间:2013-06-26 23:42:48

标签: coldfusion

我有一串数据需要在类似于表单的结构中解析和填充。

Feed中的数据如下:

CHK_7F9CFAE8-155D-D714-405CFA5B0120F968=3&CHK_7F9CFB3B-155D-D714-40F779AE41517787=3&CHK_7F9CFBDC-155D-D714-40371A9ED516A4D3=3&CHK_7F9CFBFC-155D-D714-40A4484DFB1C4825=3

我希望数据的结构如下:

STRUCT

CHK_7F9CFAE8-155D-D714-405CFA5B0120F968   |   3
CHK_7F9CFB3B-155D-D714-40F779AE41517787   |   3
CHK_7F9CFBDC-155D-D714-40371A9ED516A4D3   |   3
CHK_7F9CFBFC-155D-D714-40A4484DFB1C4825   |   3

Fieldnames   |    CHK_7F9CFAE8-155D-D714-405CFA5B0120F968,CHK_7F9CFB3B-155D-D714-40F779AE41517787,CHK_7F9CFBDC-155D-D714-40371A9ED516A4D3,CHK_7F9CFBFC-155D-D714-40A4484DFB1C4825

有没有办法用coldfusion做到这一点?

2 个答案:

答案 0 :(得分:2)

放手一搏。 已编辑:会计多字符分隔符。

    function structurize(required string data){
        var struct = {};
        var pairs = listToArray(replace(data, '&', '&', 'all'), '&');
        for (var pair in pairs){
            struct[listFirst(pair, '=')] = listLast(pair, '=');
        } 
        struct.fieldnames = structKeyList(struct);
        return struct;
    }
    writeDump(structurize('CHK_7F9CFAE8-155D-D714-405CFA5B0120F968=3&CHK_7F9CFB3B-155D-D714-40F779AE41517787=3&CHK_7F9CFBDC-155D-D714-40371A9ED516A4D3=3&CHK_7F9CFBFC-155D-D714-40A4484DFB1C4825=3'));

答案 1 :(得分:2)

是的,可以做到。

    <cfset list1 = "CHK_7F9CFAE8-155D-D714-405CFA5B0120F968=3&amp;CHK_7F9CFB3B-155D-D714-40F779AE41517787=3&amp;CHK_7F9CFBDC-155D-D714-40371A9ED516A4D3=3&amp;CHK_7F9CFBFC-155D-D714-40A4484DFB1C4825=3">

<cfset list1 = "CHK_7F9CFAE8-155D-D714-405CFA5B0120F968=3&amp;CHK_7F9CFB3B-155D-D714-40F779AE41517787=3&amp;CHK_7F9CFBDC-155D-D714-40371A9ED516A4D3=3&amp;CHK_7F9CFBFC-155D-D714-40A4484DFB1C4825=3">

<cfset myStruct = structNew()>
<cfloop list="#list1#" delimiters="&amp;" index="i">
    <cfset 'mystruct.#replace(listGetAt(i,1,"="),'-','_','all')#'  = listGetAt(i,2,"=")>
</cfloop>

<cfdump var="#myStruct#">