【问题标题】:Joining a JSON string array to a single string in ARM Templates将 JSON 字符串数组连接到 ARM 模板中的单个字符串
【发布时间】:2021-11-08 17:48:13
【问题描述】:

我正在尝试将字符串数组加入到单个字符串中。我正在寻找相当于

string.Join(string.Empty, myApimPolicyArray)

这是我的问题的最小可重现示例

逗号-Repro.json:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    },
    "variables": {
        "MyApimPolicyArray": [
            "<set-body>@{ return null; ",
            "}</set-body>"
        ]
    },
    "resources": [
    ],
    "outputs": {
        "PolicyConcat": {
            "type": "Array",
            "value": "[concat(variables('MyApimPolicyArray'))]"
        },
        "PolicyString": {
            "type": "string",
            "value": "[string(variables('MyApimPolicyArray'))]"
        }
    }
}

使用 Az 运行以下 PS:

New-AzDeployment -TemplateFile .\Comma-Repro.json -Location eastus

我尝试过诸如string()concat() 之类的模板函数,但它们并没有给我预期的结果。他们都给我结果,包括方括号、双引号、json 中的逗号。

例如字符串给了我["&lt;set-body&gt;@{ return null; ","}&lt;/set-body&gt;"]

我的期望是应该有办法得到以下结果

&lt;set-body&gt;@{ return null; }&lt;/set-body&gt;

【问题讨论】:

    标签: azure-devops azure-resource-manager azure-api-management arm-template


    【解决方案1】:

    谢谢bmoore-msft。发布您的建议作为帮助其他社区成员的答案。

    如果不想要方括号,可以使用replace()函数

    "type": "string",
    "value": "[replace(parameters(('MyApimPolicyArray'), '[]', '')]"
    "value": "[replace(parameters(('MyApimPolicyArray'), ',', '')]"
    

    可以参考How do I join array into a string in ARM template?String functions for ARM templates - replace()

    【讨论】:

    • 我的源字符串中有逗号和其他特殊字符。不幸的是,如果我必须使用替换,它会像这样"value": "[replace(replace(replace(replace(replace(string(parameters('arg')), ',\",\"', ','), '\",\"', ''), '[\"', ''), '\"]', ''), '\\\"', '\"')]" 这看起来像一个错误。
    • 对此有任何更新吗?您找到解决方案或解决方法了吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    相关资源
    最近更新 更多