解析 JSON Postman 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51025422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:07:53  来源:igfitidea点击:

Parse JSON Postman response

jsonrestapipostman

提问by Giorgio Toki

I have a test in Postman where I do a post request and need to parse the json response

我在 Postman 中进行了一个测试,我在其中进行了发布请求并需要解析 json 响应

The response looks like this:

响应如下所示:

"aPIProxy" : [ {
    "name" : "SFDC-UpdateLoginTime-v1",
    "revision" : [ {
      "configuration" : {
        "basePath" : "/",
        "steps" : [ ]
      },
      "name" : "1",...some attributes}]

and i need to get something like :

我需要得到类似的东西:

"name" : "SFDC-UpdateLoginTime-v1"
"name" : "1"

for a multiple occurrence json file.

对于多次出现的 json 文件。

采纳答案by Veeresham Tammali

The below postman script might help you.

下面的邮递员脚本可能会对您有所帮助。

var jsonData = JSON.parse(responseBody);
var jsonNamesData = jsonData.aPIProxy;
console.log(jsonNamesData);
var parsedData = "";
for(var i=0;i<jsonNamesData.length;i++){
    parsedData = parsedData +"\"name\" : \"" +jsonNamesData[i].name+"\", ";
    console.log("\"name\" : \"" +jsonNamesData[i].name+"\"");
}
console.log(parsedData);
postman.setEnvironmentVariable("parsedNamesResponse", parsedData); // updating parsed data to the environment variable parsedNamesResponse

回答by Danny Dainton

You could capture multiple 'name' properties using the _.map()function of Lodash, which is a built it module on the native application. I've had to modify what you need slightly as the namekey would have been a duplicate.

您可以使用_.map()Lodash的函数捕获多个“名称”属性,Lodash 是本机应用程序上的内置模块。我不得不稍微修改你需要的东西,因为name密钥是重复的。

const result = _.map(pm.response.json().aPIProxy, data => ({
  name: data.name,
  revisionName: data.revision[0].name
}))

pm.environment.set("response", JSON.stringify(result))

This would then store all the values in an environment variable for you to use elsewhere in another request.

然后,这会将所有值存储在一个环境变量中,供您在另一个请求中的其他地方使用。

Postman

邮差