gpt4 book ai didi

Azure API 管理策略 - 使用条件语句 - 对于有效负载

转载 作者:行者123 更新时间:2023-12-03 03:35:09 24 4
gpt4 key购买 nike

我正在使用 C# 语法来转换有效负载主体 使用条件语句来转换主体,如果可能的话,我想使用条件语句来转换有效负载主体的一个键。
下面的代码显示了该场景的更多说明。我尝试实现我想要实现的目标,但出现错误。
不确定这是否只是我的语法,或者将逻辑放在那里是否有意义。

如果这是有效负载:

{
"dependentee_name": "Steve",
"dependentee_last_name": "Rogers",
"dependentee_comment": "This is test",
"dependentee_relationship_primary": "Parent",
"dependentee_relationship_secondary": "null",
"insurer_name": "Steve",
"insurer_last_name": "Rogers",
"insurer_comment": "This is test",
"extra_info": "This is comments"
}

如有效负载中所示,有一个关键 dependentee_relationship_primary ,其值为 Parent,因此预期的转换主体如下:

{
"dependentee_info": {
"name": "Steve",
"last_name": "Rogers"
},
"insurer_info": {
"i_name": "Tony",
"i_last_name": "Stark"
},
"extra_info": "This is comments",
"relationship_type": "Parent"
}

如果这是有效负载:

{
"dependentee_name": "Steve",
"dependentee_last_name": "Rogers",
"dependentee_comment": "This is test",
"dependentee_relationship_primary": "null",
"dependentee_relationship_secondary": "Uncle",
"insurer_name": "Steve",
"insurer_last_name": "Rogers",
"insurer_comment": "This is test",
"extra_info": "This is comments"
}

如有效负载所示,有一个键 dependentee_relationship_secondary ,其值为 Uncle,因此预期的转换主体如下:

{
"dependentee_info": {
"name": "Steve",
"last_name": "Rogers"

},
"insurer_info": {
"i_name": "Tony",
"i_last_name": "Stark"
},
"extra_info": "This is comments",
"relationship_type": "Uncle"
}

到目前为止我已经尝试过该代码,但遇到了错误

<policies>
<inbound>
<base />
<return-response>
<set-status code="200" reason="ok" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var body = context.Request.Body.As<JObject>(true);

var transformedBody = new JObject();
transformedBody["extra_info"] = body["extra_info"];

var dependentee= new JObject();
dependetee["name"] = body["dependtee_name"];
dependentee["lastName"] = body["depdentee_last_name"];
transformedBody["dependtee_info"] = dependentee;

var insurer_info = new JObject();
dependetee["i_name"] = body["insurer_name"];
dependentee["i_last_name"] = body["insurer_last_name"];
transformedBody["insurer_info"] = insurer_info;

if (body["dependentee_relationship_primary"] !=null)
{
transformedBody["relationship_type"] = body["dependentee_relationship_primary"];
}

else if (body["dependentee_relationship_secondary"] !=null) {
transformedBody["relationship_type"] = body["adultrelationship"];
}

return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>

最佳答案

首先,最好修复长期存在的拼写错误问题,例如我已经为您修复的 dependetee:

Azure API Management (Policies)

你必须检查是否

  • 主体有一个 child dependentee_relationship_primary
  • 属性dependentee_relationship_primary不为null
  • 属性dependentee_relationship_primary不是空字符串
if (body.ContainsKey("dependentee_relationship_primary") && body["dependentee_relationship_primary"].Type != JTokenType.Null && !string.IsNullOrEmpty(body["dependentee_relationship_primary"].Value<string>()))
{
transformedBody["relationship_type"] = body["dependentee_relationship_primary"];
}

完整的政策:

<policies>
<inbound>
<base />
<return-response>
<set-status code="200" reason="ok" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var body = context.Request.Body.As<JObject>(true);

var transformedBody = new JObject();
transformedBody["extra_info"] = body["extra_info"];

var dependentee= new JObject();
dependentee["name"] = body["dependtee_name"];
dependentee["lastName"] = body["depdentee_last_name"];
transformedBody["dependtee_info"] = dependentee;

var insurer_info = new JObject();
insurer_info["i_name"] = body["insurer_name"];
insurer_info["i_last_name"] = body["insurer_last_name"];
transformedBody["insurer_info"] = insurer_info;

if (body.ContainsKey("dependentee_relationship_primary") && body["dependentee_relationship_primary"].Type != JTokenType.Null && !string.IsNullOrEmpty(body["dependentee_relationship_primary"].Value<string>()))
{
transformedBody["relationship_type"] = body["dependentee_relationship_primary"];
}
else if (body.ContainsKey("dependentee_relationship_secondary") && body["dependentee_relationship_secondary"].Type != JTokenType.Null && !string.IsNullOrEmpty(body["dependentee_relationship_secondary"].Value<string>()))
{
transformedBody["relationship_type"] = body["adultrelationship"];
}

return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>

结果:

enter image description here

顺便说一句:
你的条件没有意义:没有定义adultrelationship

关于Azure API 管理策略 - 使用条件语句 - 对于有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73616553/

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