如何在ColdFusion< 2016中的结构中保留分配顺序 [英] How can I preserve the assign order in a structure in ColdFusion <2016
问题描述
ColdFusion不会保留将元素分配给结构的顺序.
在我的特定情况下,这需要我从该结构创建SOAP信封,而接收方需要按特定顺序在信封中进行数据处理.
从ColdFusion 2016及更高版本开始,StructNew()方法中提供了一个类型"选项,您可以在其中指定订购".不幸的是,我仍在使用CF9.02 ...例如(在CF9中):
ColdFusion doesn't preserve the order in which elements were assigned to the structure.
This gives a problem when, in my particular case, I need to create a SOAP envelope from this structure and the receiver needs to data in the envelope in a particular order.
Starting from ColdFusion 2016 and higher, there is a "type" option in the StructNew() method where you can specify "Ordered". Unfortunately, I'm still on CF9.02...
For example (in CF9):
<cfscript>
myStruct=StructNew();
myStruct.one=StructNew();
myStruct.one.item1="Old Trafford";
myStruct.one.item2="Anfield";
myStruct.two=StructNew();
myStruct.two.item3="Stamford Bridge";
myStruct.two.item4="Villa Park";
myStruct.three=StructNew();
myStruct.three.item5="St James Park";
myStruct.three.item6="Emirates Stadium";
WriteDump(myStruct);
</cfscript>
输出:
{"ONE":{"ITEM2":"Anfield","ITEM1":"Old Trafford"},"THREE":{"ITEM6":"Emirates Stadium","ITEM5":"St James Park"},"TWO":{"ITEM4":"Villa Park","ITEM3":"Stamford Bridge"}}
如您所见,订单被打乱了.
As you can see, the order is scrambled.
第二个问题是大写.所有项目都转换为大写...
对于SOAP信封,这是一个问题,因为它区分大小写.
A second problem is the UPPERCASE. All the items are transformed to uppercase...
This is a problem for a SOAP envelope, because it is case sensitive.
推荐答案
-
订购.
解决方案是使用Java LinkedHashMap,它可以像ColdFusion中的结构一样使用.
所以,我只需要更换StructNew();
使用:createObject("java","java.util.LinkedHashMap").init();
大写:
通过其字符串名称分配/寻址它们.这样可以保留大小写.
Uppercase:
Assign/address them though their string name. This will preserve the case.
因此,示例变为:
<cfscript>
myStruct=createObject("java", "java.util.LinkedHashMap").init();
myStruct['one']=createObject("java", "java.util.LinkedHashMap").init();
myStruct['one']['item1']="Old Trafford";
myStruct['one']['item2']="Anfield";
myStruct['two']=createObject("java", "java.util.LinkedHashMap").init();
myStruct['two']['item3']="Stamford Bridge";
myStruct['two']['item4']="Villa Park";
myStruct['three']=createObject("java", "java.util.LinkedHashMap").init();
myStruct['three']['item5']="St James Park";
myStruct['three']['item6']="Emirates Stadium";
WriteDump(SerializeJSON(myStruct));
</cfscript>
输出:
{"one":{"item1":"Old Trafford","item2":"Anfield"},"two":{"item3":"Stamford Bridge","item4":"Villa Park"},"three":{"item5":"St James Park","item6":"Emirates Stadium"}}
井井有条并保留大小写!
Nicely ordered and case preserved!
这篇关于如何在ColdFusion< 2016中的结构中保留分配顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!