-1

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

数据来自一个提要如下:

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

我希望数据的结构如下:

结构

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

有没有办法通过冷融合来做到这一点?

4

2 回答 2

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'));
于 2013-06-27T00:05:35.187 回答
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#"> 
于 2013-06-27T00:09:28.003 回答