JSON只返回一个对象

时间:2017-09-15 07:47:23

标签: javascript json

我正在使用Domo从API中提取用户。我正在尝试拉动所有用户并在网站中包含自定义字段中的附加条目。截至目前,它只拉动一个用户。此外,在obj2日志中,它只返回一个对象。因为它没有返回所有对象,所以它永远不会获得ID 4,即Department。它只输出ID 11,这是我们使用和停止的一种ID。

我觉得我很接近......我需要修理什么指针?

我是JavaScript的新手,善良!

if(metadata.report == 'User'){
DOMO.log('metadata.reportname: ' +  metadata.report);
var base_url = 'https://app.com/api/author/users/';
var res = httprequest.get(base_url);
//DOMO.log('res: ' + res);
var raw = JSON.parse(res);
datagrid.addColumn('id', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('hris_id', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('full_name', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('first_name', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('last_name', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('department', datagrid.DATA_TYPE_STRING);

var users = raw.users;
//DOMO.log('users: ' + JSON.stringify(users));

for(var i = 0; i < users.length; i++){
  var obj = users[i];
}
  //make new specific request with includes
  var temp = obj.id;
  var temp2 = '?includes[]=custom_fields';
  var uniq_url = base_url.concat(temp.concat(temp2));
  //DOMO.log(uniq_url);
  var res2 = httprequest.get(uniq_url);
  var raw2 = JSON.parse(res2);
  //DOMO.log((raw2));

  var linked = raw2.linked;
  var cf = linked.custom_fields;
  var cfv = linked.custom_field_values;

 for(var g = 0; g < cfv.length; j++){
    var obj2 = cfv[g];
    DOMO.log(obj2.value);
  }
    if (obj2.custom_fields.id == 4){ 
    DOMO.log("Department is: " + obj2.value);
    }

    //if (obj2.links.custom_field.id == 5){
      //DOMO.log('Position is ' + obj2.value);


  //DOMO.log('JSON ' + res);
  //DOMO.log('id: ' + obj.id);
  datagrid.addCell(obj.id);
  //DOMO.log('hris_id: ' + obj.hris_id);
  datagrid.addCell(obj.hris_id);   
  //DOMO.log('full_name: ' + obj.hris_id);
  datagrid.addCell(obj.full_name);          
  //DOMO.log('first_name: ' + obj.first_name);
  datagrid.addCell(obj.first_name);
  //DOMO.log('last_name: ' + obj.last_name)
  datagrid.addCell(obj.last_name);
  //DOMO.log('value: ' + obj.value);
  datagrid.addCell(obj.value);
  datagrid.endRow();
}  

//Console Log;
metadata.reportname: User
https://app.com/api/author/users/976?includes[]=custom_fields
{"id":"3785","value":"12870","links":{"custom_field":{"id":"11","type":"custom_fields"}}}

1 个答案:

答案 0 :(得分:1)

问题在于for循环:

for(var i = 0; i < users.length; i++){
  var obj = users[i];
}

您正在迭代所有用户,但始终使用当前[i]用户。

我猜你的意思是括号将包围整个代码:

像这样:

var users = raw.users;
//DOMO.log('users: ' + JSON.stringify(users));
for (var i = 0; i < users.length; i++) {
  var obj = users[i];

  //make new specific request with includes
  var temp = obj.id;
  var temp2 = '?includes[]=custom_fields';
  var uniq_url = base_url.concat(temp.concat(temp2));
  //DOMO.log(uniq_url);
  var res2 = httprequest.get(uniq_url);
  var raw2 = JSON.parse(res2);
  //DOMO.log((raw2));

  var linked = raw2.linked;
  var cf = linked.custom_fields;
  var cfv = linked.custom_field_values;

  for (var g = 0; g < cfv.length; j++) {
    var obj2 = cfv[g];
    DOMO.log(obj2.value);
  }
  if (obj2.custom_fields.id == 4) {
    DOMO.log("Department is: " + obj2.value);
  }
}

编辑: 在你的额外输入后,我看到下一个for循环中的另一个错误 - 同样的问题,你正在迭代它但最终取一个值(最后一个)并且有一个错误,因为j没有定义,因为你正在使用g。 / p>

固定代码:

  if (metadata.report == 'User') {
    DOMO.log('metadata.reportname: ' + metadata.report);
    var base_url = 'https://app.com/api/author/users/';
    var res = httprequest.get(base_url);
    //DOMO.log('res: ' + res);
    var raw = JSON.parse(res);
    datagrid.addColumn('id', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('hris_id', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('full_name', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('first_name', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('last_name', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('department', datagrid.DATA_TYPE_STRING);

    var users = raw.users;
    //DOMO.log('users: ' + JSON.stringify(users));

    for (var i = 0; i < users.length; i++) {
      var obj = users[i];
      //Move brackets to end

      //make new specific request with includes
      var temp = obj.id;
      var temp2 = '?includes[]=custom_fields';
      var uniq_url = base_url.concat(temp.concat(temp2));
      //DOMO.log(uniq_url);
      var res2 = httprequest.get(uniq_url);
      var raw2 = JSON.parse(res2);
      //DOMO.log((raw2));

      var linked = raw2.linked;
      var cf = linked.custom_fields;
      var cfv = linked.custom_field_values;

      //for (var g = 0; g < cfv.length; j++) { --> g++ instead of j++
      for (var g = 0; g < cfv.length; g++) {
        var obj2 = cfv[g];
        DOMO.log(obj2.value);
        //Move bracket to after using obj2
        //Added more conditions:
        if (obj2.custom_fields.id == 4 || obj2.custom_fields.id==5 || obj2.someOtherProp=="Something") {
          DOMO.log("Department is: " + obj2.value);
          //DOMO.log('JSON ' + res);
          //DOMO.log('id: ' + obj.id);
          datagrid.addCell(obj.id);
          //DOMO.log('hris_id: ' + obj.hris_id);
          datagrid.addCell(obj.hris_id);
          //DOMO.log('full_name: ' + obj.hris_id);
          datagrid.addCell(obj.full_name);
          //DOMO.log('first_name: ' + obj.first_name);
          datagrid.addCell(obj.first_name);
          //DOMO.log('last_name: ' + obj.last_name)
          datagrid.addCell(obj.last_name);
          //DOMO.log('value: ' + obj.value);
          datagrid.addCell(obj.value);
          datagrid.endRow();
        }
      }
      //if (obj2.links.custom_field.id == 5){
      //DOMO.log('Position is ' + obj2.value);

      //Moved this code inside the if id==4.

    }
  }