邮递员从回复

时间:2017-09-13 17:14:10

标签: postman

我正在使用Postman的控制台用console.log显示API调用的响应,因为我有很多迭代,所以我使用的是跑步者。但是,API响应中的很多信息都给我带来了麻烦,所以我想做的是用console.log打印responseBody的具体信息。

作为Postman的测试,我使用以下内容:

var body = JSON.parse(responseBody);
console.log(JSON.stringify(body.data));

回复是:

  

[{ “设备”: “1BED7”, “时间”:1505320342 “数据”: “05b006bcac00000000000000”, “SNR”: “21.00”, “linkQuality”: “平均”, “seqNumber”:555,” rinfos “:[{” 抽头 “:” A2A”, “延迟”:1.4, “LAT”: “53.0”, “LNG”: “2.0”},{ “抽头”: “A2B”, “延迟”:0.5 , “LAT”: “53.0”, “LNG”: “2.0”}]},{ “设备”: “1CED7”, “时间”:1505277142 “数据”: “05b006bcac00000000000000”, “SNR”: “20.68” “linkQuality”: “平均”, “seqNumber”:554, “rinfos”:[{ “抽头”: “A2C”, “延迟”:1.3, “LAT”: “53.0”, “LNG”: “2.0” },{ “抽头”: “232”, “延迟”:1.9, “LAT”: “53.0”, “LNG”: “2.0”}]},{ “设备”: “152C3”, “时间”:1505233937 “数据”: “05b006bcac00000000000000”, “SNR”: “19.14”, “linkQuality”: “平均”, “seqNumber”:553, “rinfos”:[{ “抽头”: “215”, “延迟”:2.4 , “LAT”: “53.0”, “LNG”: “2.0”}]},{ “设备”: “1BF81”, “时间”:1505190735 “数据”: “05b006bcac00000000000000”, “SNR”: “21.67” “linkQuality”: “平均”, “seqNumber”:552, “rinfos”:[{ “抽头”: “1CC”, “延迟”:2.0, “LAT”: “53.0”, “LNG”: “2.0” },{ “敲击”: “25A”, “延迟”:1.6, “LAT”: “53.0”, “LNG”: “2.0”}]},

我想用console.log打印的只是设备,时间和数据的值:

  

{ “1BED7” 1505320342 “05b006bcac00000000000000”},{ “1CED7” 1505277142 “05b006bcac00000000000000”},{ “152C3”,1505233937 “05b006bcac00000000000000”},   等等...

我的编程技巧非常有限,很抱歉,如果答案是如此明显,我已经测试了很多东西,但我仍然卡住了。

如果你能提供帮助,非常感谢

2 个答案:

答案 0 :(得分:0)

我认为你的响应是对象数组。它已经是一个json对象了。首先你做错了就是你不需要解析它。你可以直接使用它。在下面查看下面的代码片段答案结束。我认为这满足了你的需要。我使用forEach函数迭代响应数组并将你需要的值推送到空数组中。这个结果数组包含以下格式的对象。你可以访问每个属性这个数组的每个对象都是由javascript .运算符组成的。我认为这对你来说很明显     [ { "device": "1BED7", "time": 1505320342, "data": "05b006bcac00000000000000" }, { "device": "1CED7", "time": 1505277142, "data": "05b006bcac00000000000000" }, { "device": "152C3", "time": 1505233937, "data": "05b006bcac00000000000000" }, { "device": "1BF81", "time": 1505190735, "data": "05b006bcac00000000000000" } ]

var responseBody=[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},
    {"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]}];
var array=[];
responseBody.forEach(function (obj) {
    array.push({device:obj.device,time:obj.time,data:obj.data})
})
console.log(array);

答案 1 :(得分:0)

let results = _.map(JSON.parse(responseBody), 
  (sensor) => { return [sensor.device, sensor.time, sensor.data]});

// results contains an array like
// [[deviceId1, time1, data1], [deviceId1, time1, data1], ...]

console.log(results);