简体   繁体   中英

Populating a table with JSON Data

I'm doing something very wrong and I can't for the life of me figure it out. I'm a novice so that doesn't help.

    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 );
}

but the way the data is coming back is all screwey

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

在此处输入图片说明

[in progress - i'll get you a working sample just got pulled inot a meeting] first parse it to 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 );
}

I was able to get it working. Thank you everyone for your assistance.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM