用JSON数据填充表

时间:2018-07-19 13:41:40

标签: javascript json domo

我做错了什么,我一生无法解决。我是新手,所以无济于事。

    var res = httprequest.post('https://us3.api.samsara.com/v1/fleet/drivers?access_token=xxxxxxxx','{\"groupId\": 9999}');

DOMO.log('res: ' + res);

var toJson = JSON.parse(res); 


var header = lines[0].split(',');

datagrid.addColumn('id',datagrid.DATA_TYPE_STRING);
datagrid.addColumn('name',datagrid.DATA_TYPE_STRING);

for (var i = 1; i < lines.length; i++) {
  console.warn( lines.[i].id );
  console.warn( lines.[i].name );
}

但是数据返回的方式却很复杂

res: {"drivers":[{"id":12345,"name":"Brian Smith"},
{"id":23456,"name":"Bruce Lee"},
{"id":89234,"name":"Carson Wentz"},

enter image description here

2 个答案:

答案 0 :(得分:0)

[进行中-我将为您提供一个工作样本,刚刚开会] 首先将其解析为json。

var res = '{"drivers":[{"id":12345,"name":"Brian Smith"},{"id":23456,"name":"Bruce Lee"},{"id":89234,"name":"Carson Wentz"}]}';

var toJson = JSON.parse(res); 

// you actually want to be iterating the drivers array

var lines = toJson['drivers'];

for (var i = 1; i < lines.length; i++) {
  console.warn( lines[i].id );
  console.warn( lines[i].name );
}

答案 1 :(得分:0)

我能够使其正常运行。谢谢大家的帮助。

var res = httprequest.post('https://us3.api.samsara.com/v1/
fleet/drivers?access_token=xxxxx','{\"groupId\": xxxxx}');

//DOMO.log('res: ' + res);

//added .drivers to read further into the JSON file before parsing
var data = JSON.parse(res).drivers;

datagrid.addColumn('id', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('name', datagrid.DATA_TYPE_STRING);

//lets you read data
DOMO.log('data: ' + data);

for(var i = 0; i < data.length; i++){
    var drivers = data[i].properties;
    datagrid.addCell(data[i].id);
    datagrid.addCell(data[i].name);
    datagrid.endRow();
}