如何从API管理策略中的响应体获取价值?

Mar*_*ell 1 json azure azure-api-management

我目前正在使用 Azure API 管理中的策略,并且对提取从响应正文返回的值感兴趣。

<send-one-way-request mode="new">
    <set-url>http://requestb.in/xje199xj</set-url>
    <set-method>POST</set-method>
    <set-header name="Content-Type" exists-action="override">
    <value>application/json</value>
    </set-header>
        <set-body>@{

            //JObject or string?
            string response = context.Response.Body.As<string>(preserveContent: true);
            //something here..
            }
        </set-body>
    </send-one-way-request>
Run Code Online (Sandbox Code Playgroud)

响应如下所示:

"getBookableResourcesResponse": {
    "getBookableResourcesResult": {
      "hasError": false,
      "errorCode": 0,
      "BookableResource": [
        {
          "resourceCode": "TRA",
          "description": "Trailer",
          "group": "F",
          "subGroup": "C",
          "category": "R",
          "dialogType": "CARGO",
          "orgCode": "DECK",
          "length": 14,
          "taraWeight": "7000",
          "grossWeight": "25001",
          "AddResource": [
            {
              "resourceCode": "EXPFIN",
              "description": "Export Finland",
              "dialogType": "UNDEFINED",
              "amount": "0",
              "ticketType": "",
              "orgCode": "EXPFIN",
              "required": "false"
            }.....`
Run Code Online (Sandbox Code Playgroud)

我想要获取从“resourceCode”属性返回的值,在本例中为“TRA”,然后创建一个新的 JObject,我可以将其发送到我的 Azure 函数。

{
   "resourceCode": "valueFromResponseBody"
}
Run Code Online (Sandbox Code Playgroud)

Vit*_*tin 6

代替

string response = context.Response.Body.As<string>(preserveContent: true);
Run Code Online (Sandbox Code Playgroud)

尝试:

var response = context.Response.Body.As<JObject>(preserveContent: true);
Run Code Online (Sandbox Code Playgroud)

这将返回 Json.NET JObject ( http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm ),您稍后可以使用它来导航您的响应。