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

Coldfusion StructSetMetaData not updating items in an array inside a structure

我有一个名为 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"}]}

这是 JSON 发送给另一家公司的 API,所以我无法控制他们需要的结构或数据类型。每个值在转换为 JSON 时都需要是字符串,但 Coldfusion 想要在使用序列化 JSON 时将像“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>

如何为驱动程序键中的结构数组中的键设置元数据?

我会做这样的事情。对于数组内部结构,您需要以与为 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