【问题标题】:Extract json object from the response in karate framework从空手道框架中的响应中提取 json 对象
【发布时间】:2020-02-12 13:25:46
【问题描述】:

来自 API 调用的以下 JSON 响应

{ 
   "status":200,
   "data":[ 
      { 
         "items":[ 
            { 
               "type":"Notification",
               "content":{ 
                  "message":"Welcome to ACME",
                  "id":"66d93f00-4d74-11ea-9c63-6f69f79cca26",
                  "args":{ 
                     "freezeTimeline":false,
                     "month":0
                  },
                  "timelineButtonText":null
               }
            },
            { 
               "type":"Notification",
               "content":{ 
                  "message":"ACME is a small account.",
                  "id":"670928a0-4d74-11ea-9c63-6f69f79cca26",
                  "args":{ 
                     "freezeTimeline":false,
                     "month":0
                  },
                  "timelineButtonText":null
               }
            },
            { 
               "type":"Event",
               "content":{ 
                  "id":"e08ea760-5b4a-45d2-888e-4a5c8ef5bf1f"
               }
            },
            { 
               "type":"Event",
               "content":{ 
                  "id":"77b0c588-36be-4821-8d26-8c374c29a899"
               }
            },
            { 
               "type":"Event",
               "content":{ 
                  "id":"9fd74f9c-4d50-445b-94f0-37d21e53bdae"
               }
            }
         ],
         "move":{ 
            "Month":1,
            "Year":1,
            "Quarter":1
         }
      }
   ]
}json

Items数组里面,type=Event有3项 如何获取第一个事件的 content.id?

它对我有用,就像下面的代码一样

And def eventId = response.data[0].items[4].content.id

但我想要类似的东西:如果有一种“事件”提取相同的“content.id”。

【问题讨论】:

标签: json karate


【解决方案1】:

分两步完成,阅读/遵循会更容易:

假设items 数组在一个变量中:

* def events = $items[?(@.type=='Event')]
* def first = events[0].content.id
* match first == 'e08ea760-5b4a-45d2-888e-4a5c8ef5bf1f'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多