调用WebRequest\rest方法使用转义符破坏json字符串

调用WebRequest\rest方法使用转义符破坏json字符串,json,powershell,azure,Json,Powershell,Azure,我的字符串: {"properties":{"template":{"contentVersion":"1.0.0.0","$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","resources":"{"type":"Microsoft.Network/networkSecurityGroups","name":"[parameters('GroupName')]"

我的字符串:

{"properties":{"template":{"contentVersion":"1.0.0.0","$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","resources":"{"type":"Microsoft.Network/networkSecurityGroups","name":"[parameters('GroupName')]","apiVersion":"2016-03-30","location":"[resourceGroup().Location]","properties":{"securityRules":["@{name=DenyAll; properties=}"]}}","parameters":"{"GroupName":{"defaultValue":"GroupName","type":"String"}}"},"mode":"Incremental"}}
Powershell在执行invoke webrequest\restmethod时会这样处理:

\"{\"properties\":{\"template\":{\"contentVersion\":\"1.0.0.0\",\"$schema\":\"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#\",\"resources\":\"{\"type\":\"Microsoft.Network/networkSecurityGroups\",\"name\":\"[parameters('GroupName')]\",\"apiVersion\":\"2016-03-30\",\"location\":\"[resourceGroup().Location]\",\"properties\":{\"securityRules\":[\"@{name=DenyAll; properties=}\"]}}\",\"parameters\":\"{\"GroupName\":{\"defaultValue\":\"GroupName\",\"type\":\"String\"}}\"},\"mode\":\"Incremental\"}}\"
这让Azure非常不开心。如何避免这种情况

我的电话:

iwr $url -Headers $headers -Body $body -Method Put -ContentType 'application/json'
基本上我必须这样做:

$(get-content path -Raw -ReadCount 0) | ConvertFrom-Json

然后使用它来传递它,首先检查两件事,JSON对象的主体,并验证字符串是否符合JSON结构

我在您的初始字符串中发现错误,JSON中有效的格式为:

{"properties":{"template": {"contentVersion": "1.0.0.0","$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","resources": {"type": "Microsoft.Network/networkSecurityGroups","name": "[parameters('GroupName')]","apiVersion":"2016-03-30","location":"[resourceGroup().Location]","properties": {"securityRules": ["@{name=DenyAll; properties=}"]}},"parameters":{"GroupName":{"defaultValue":"GroupName","type": "String"}}},"mode":"Incremental"}}
此转换可在以下位置完成: 现在,您可以使用任何常用编辑器(如记事本+)来转义字符串,并加入一个简单的行,最后在url中转义字符串

另一个建议是,如果您需要测试呼叫某些服务,请使用邮递员

然后再打一次电话。
问候。

谢谢,确实那个字符串是无效的,这让我怀疑它是否会再次出现,所以我的json处理得很糟糕
 {\"properties\": {\"template\": {\"contentVersion\": \"1.0.0.0\",\"$schema\": \"https:\/\/schema.management.azure.com\/schemas\/2015-01-01\/deploymentTemplate.json#\",\"resources\": {\"type\": \"Microsoft.Network\/networkSecurityGroups\",\"name\": \"[parameters('GroupName')]\",\"apiVersion\": \"2016-03-30\",\"location\": \"[resourceGroup().Location]\",\"properties\": {\"securityRules\": [\"@{name=DenyAll; properties=}\"]}},\"parameters \": {\"GroupName\": {\"defaultValue\": \"GroupName\",\"type\": \"String\"}}},\"mode \": \"Incremental\"}}