gpt4 book ai didi

Coldfusion StructSetMetaData 不更新结构内数组中的项目

转载 作者:行者123 更新时间:2023-12-03 18:47:44 25 4
gpt4 key购买 nike

我有一个名为 requestStruct 的结构,如下所示:

{"api_version":"1","SourceId":"99","CampaignId":"999","policy":{"zip":"60644","street_address":"43 Memory Lane","email":"test@test.com","currently_insured":"true","coverage_level":"2","current_carrier":"28","current_insured_duration":"7"},"drivers":[{"first_name":"Bat","last_name":"Man","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Boy","last_name":"Wonder","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Cat","last_name":"Woman","birth_date":"1966-06-06","homeowner":"true","sr22":"false"},{"first_name":"Bat","last_name":"Girl","birth_date":"1966-01-01","homeowner":"true","sr22":"false"}]}


这是发送到另一家公司 API 的 JSON,因此我无法控制他们所需的结构或数据类型。每个值在转换为 JSON 时都需要是字符串,但 Coldfusion 想要在使用 serializeJSON 时将像“99”这样的字符串转换为数字 99。所以我使用 StructSetMetaData 来确保它们被转换为 JSON 作为字符串。
这些语句工作正常:
<cfscript>                   
metadata = {
api_version: {type:"string",name:"api_version"},
SourceId: {type:"string",name:"SourceId"},
CampaignId: {type:"string",name:"CampaignId"}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>

<cfscript>
metadata = {
policy:{
keys:{
"zip":{type:"string",name:"zip"},
"email":{type:"string",name:"email"},
"currently_insured":{type:"string",name:"currently_insured"},
"coverage_level":{type:"string",name:"coverage_level"},
"current_carrier":{type:"string",name:"current_carrier"},
"current_insured_duration":{type:"string",name:"current_insured_duration"}
}
}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>
但是我无法弄清楚如何对结构的驱动程序部分执行相同的操作,因为它包含一个数组。
我试过这个,但没有效果:
<cfscript>
metadata = {
drivers:{
keys:{
"homeowner":{type:"string",name:"homeowner"},
"sr22":{type:"string",name:"sr22"}
}
}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>
我试过这个,但没有效果:
<cfscript>
metadata = {
drivers:{
keys:{
"homeowner[0]":{type:"string",name:"homeowner"},
"sr22[0]":{type:"string",name:"sr22"}
}
}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>
如何为驱动程序 key 中结构数组中的 key 设置元数据?

最佳答案

我会做这样的事情。对于数组内部结构,您需要以与为 arraySetMetaData 提供元数据相同的方式提供元数据。功能。

<cfset requestStruct = {"api_version":"1","SourceId":"99","CampaignId":"999","policy":{"zip":"60644","street_address":"43 Memory Lane","email":"test@test.com","currently_insured":"true","coverage_level":"2","current_carrier":"28","current_insured_duration":"7"},"drivers":[{"first_name":"Bat","last_name":"Man","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Boy","last_name":"Wonder","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Cat","last_name":"Woman","birth_date":"1966-06-06","homeowner":"true","sr22":"false"},{"first_name":"Bat","last_name":"Girl","birth_date":"1966-01-01","homeowner":"true","sr22":"false"}]}>
<cfscript>
metadata = {
api_version: {type:"string",name:"api_version"},
SourceId: {type:"string",name:"SourceId"},
CampaignId: {type:"string",name:"CampaignId"},
policy:{
keys:{
"zip":{type:"string",name:"zip"},
"email":{type:"string",name:"email"},
"currently_insured":{type:"string",name:"currently_insured"},
"coverage_level":{type:"string",name:"coverage_level"},
"current_carrier":{type:"string",name:"current_carrier"},
"current_insured_duration":{type:"string",name:"current_insured_duration"}
}
}
};
driversMetadata = {
items: []
};
for(driver in requestStruct.drivers){
driversMetadata.items.append({
"homeowner":{type:"string", name:"homeowner"},
"sr22":{type:"string", name:"sr22"}
});
}
metadata.drivers = driversMetadata;
StructSetMetaData(requestStruct, metadata);
</cfscript>
DEMO

关于Coldfusion StructSetMetaData 不更新结构内数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67626106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com